• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Birthday field start and end year

Birthday field start and end year 11 years 3 months ago #26536

I would like to set start year just 3 years back and up until todays date.

For this specific form all the details registered will be for kids between 0-3 years old.

I see that there is a way of doing so with the calendar field, but I have not found a way of doing this with the birthday field?
The administrator has disabled public write access.

Birthday field start and end year 9 years 2 months ago #34609

Did you sort this, I need the same
The administrator has disabled public write access.

Birthday field start and end year 7 years 7 months ago #37427

  • mylo
  • mylo's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 13
  • Thank you received: 3
I needed this functionality and thought that I'd share what works for me. In my case, I need a birthdate year field that ranges from the current year to 100 years ago.

Please note: What I am about to explain is editing the form's core code and will be overwritten when you update. I have a way to combat that though. It also assumes that all birthday year fields will be the same.

Open the following file:
joomla_root/administrator/components/com_rsform/helpers/fields/birthday.php

On (or about) line 181 you should see this code...
$start = (int) $this->getProperty('STARTYEAR', 1970);
$end = (int) $this->getProperty('ENDYEAR', 2050);

For my case, I changed it to...
$start = (int) date('Y'); // current year
$end = (int) date('Y', strtotime('-100 years')); // 100 years ago

To keep a copy of my changes after an RSForm update, I just made a copy of the file and renamed it 'birthday-copy.php'.
The administrator has disabled public write access.

Birthday field start and end year 7 years 4 weeks ago #38128

I'm not sure why I havent gotten mails when there has been new replies to this post, but, yes I managed to solve this.

The solution no longer works, in newer versions of RSform.

As I'm doing an update of the site where I first used this, I'm looking to update the code as well.

The copied code was placed in the "CSS and Javascript" area, under Javascript:
<script type="text/javascript">
window.addEvent('domready', function(){

document.getElementById('fdatoy').options.length = 0;

year = new Date().getFullYear();

min = year-3;
max = year;
select = document.getElementById('fdatoy');

for (var i = min; i<=max; i++){
opt = document.createElement('option');
opt.value = i;
opt.text = i;
select.appendChild(opt);
}
});
</script>

Perhaps developers could help out modifing this code to work with newer versions of Rsform?
The administrator has disabled public write access.

Birthday field start and end year 7 years 4 weeks ago #38129

Working code as of now, with the great help from Adrian on support:

The field name is fdate. But you add y after it to represent year.
I havent tried, but I guess you can do the same with both m (month) and d (day) if needed.

<script type="text/javascript">
window.onload = function(){

document.getElementById('fdatoy').options.length = 0; // Change here

year = new Date().getFullYear();

min = year-3; // Change to get your range
max = year; // Change to get your range i.e. year+10 (ten years from now)
select = document.getElementById('fdatoy'); // Change here

for (var i = min; i<=max; i++){
opt = document.createElement('option');
opt.value = i;
opt.text = i;
select.appendChild(opt);
}
};
</script>


Thanks Adrian!
The administrator has disabled public write access.
The following user(s) said Thank You: mylo

Birthday field start and end year 5 years 8 months ago #39452

  • mylo
  • mylo's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 13
  • Thank you received: 3
Thanks for updating this.

If you prefer to have the year options listed in descending order, just change the "for" statement to begin counting from the max number and subtract one with each iteration until the min number is reached...
for (var i=max; i>=min; i--)
Last Edit: 5 years 8 months ago by mylo.
The administrator has disabled public write access.
The following user(s) said Thank You: kenneth.c.koppervik

Birthday field start and end year 5 years 2 weeks ago #40137

  • pscharfg
  • pscharfg's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Is there anyway to keep the 'year' placeholder when using this code? Thanks in advance.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40138

Hi. I'm not sure I follow and understand what your desired result is.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40142

  • pscharfg
  • pscharfg's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
I'm sorry for not being clear. I am creating a fairly complex intake form for a medical practice. I have the same need to provide a limit of start and end years as described in the posts above. In my case, I also set the limit to 100 years back from today (patients can't be older than 100). The field type is a Birthday Field. If the patient is not 18 years old, we need to require a parent or guardian information (a separate issue - not sure how to deal with this at all yet).

The default Birthday Field is presented with placeholders as 'DAY | MONTH | YEAR' - but when I use the script above its shows 'DAY | MONTH | 2020' where 2020 is no longer a placeholder, it is a value due to the script.

Thanks for taking the time to try and help.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40144

  • mylo
  • mylo's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 13
  • Thank you received: 3
Can you paste your code here? I suspect there is something off. Do you get a JS error in the console of your browser?
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40145

  • pscharfg
  • pscharfg's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
No errors in the console and the script seems to be working with the exception of the placeholder. Is there anything that needs to be done directly to the field properties other than the trigger in the attributes field? I doble checked that "Show 'Please Select' on Year Selector - leave blank to remove" field has "Year" in it.

<script type="text/javascript">
window.onload = function(){
document.getElementById('date-of-birthy').options.length = 0; // Change here
year = new Date().getFullYear();
min = year-3; // Change to get your range
max = year; // Change to get your range i.e. year+10 (ten years from now)
select = document.getElementById('date-of-birthy'); // Change here
for (var i=max; i>=min; i--) {
opt = document.createElement('option');
opt.value = i;
opt.text = i;
select.appendChild(opt);
}
};
</script>
Last Edit: 5 years 2 weeks ago by pscharfg.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40147

  • mylo
  • mylo's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 13
  • Thank you received: 3
Now that I'm looking at my code, I think that I had that same issue. The script is stripping out all of the options that are originally written when the page loads. So I just added 'Year' back as the first option.
for (var y=2; y<=10; y++) {
        select = document.getElementById('DOB'+y+'y');
        opt1 = document.createElement('option');
        opt1.value = '';
        opt1.text = 'Year';
        select.appendChild(opt1);
        for (var i=min; i>=max; i--){
            opt2 = document.createElement('option');
            opt2.value = i;
            opt2.text = i;
            select.appendChild(opt2);
        }
      }

Explanation of my code
I have 10 DOB fields that have ids 'DOB1y', 'DOB2y', 'DOB3y', etc. My first DOB has to be an 18 year old person or older so its code is handled by a different loop. The rest of the DOBs can be any age, but that's why my first 'for' loop starts with a 2. In the first 'for' loop above, I'm creating the first option to be the word 'Year'. It has a value of '' so that if it's selected, it will trigger an error. Then I start the 2nd 'for' loop to take care of all of the other options like the original code showed.
The administrator has disabled public write access.
The following user(s) said Thank You: pscharfg

Birthday field start and end year 5 years 2 weeks ago #40148

  • pscharfg
  • pscharfg's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Excellent write-up and explanation - Thank you!

Now I have to figure out how to dynamically make a field "required" if the age is less than 18. Any ideas? Again thanks for going out of your way to help.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40149

  • mylo
  • mylo's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 13
  • Thank you received: 3
Since you can only use a dropdown field to manipulate conditional fields, I would suggest creating a dropdown field whose value is set by a Javascript function. Then you can use it as the conditional test.

So...
  1. Create a dropdown field called Under18 with 2 Items: True and False.
  2. In Under18, go to Attributes > Additional Attributes, put
    style="display: none"
    This will hide the field from view.
  3. Save
  4. In your birthdate field, go to Attributes > Additional Attributes, put
    onchange="calcAge()"
    This will call the Javascript function to calculate the age which then sets the value for Under18.
  5. Go to Form Properties > CSS & Javascript > Javascript area.
  6. Create a function called calcAge() that does your age calculation and sets the value of the Under18 field to True or False.
  7. Create the required field that will be shown if the age is calculated as less than 18.
  8. Go to Form Properties > Conditional Fields > New Condition
  9. Create the condition to show the required field if Under18 is True.
Last Edit: 5 years 2 weeks ago by mylo.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40152

  • pscharfg
  • pscharfg's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Again, I want to thank you for the time you are putting into this. I like your creativity when coming up with solutions.

I may have been going down the wrong path - I was trying to chase down a javascript solution that evaluated the age and then manually changed the "required" attribute (if one even exists) and the css class if the age was under 18. I can see how to invalidate using php but that appears to invalidate both 'required/mandatory' and the form field validation. The PHP solution would only work on POST as well.

Your solution is very interesting. I am going to give it a try and report back.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40155

  • pscharfg
  • pscharfg's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Mylo, your method was successful and has openend my eyes to a new way of solving some of the challenges I was having. However, I am having an unexpected problem - it appears that programmatically changing the value (which works well) in the dropdown list doesn't trigger the conditional field. However if I manually change that drop down (I made it visible to test) all works as planned. Thoughts? Your help is very much appreciated.

After the calculation script the following script runs. I wonder if 'selected' is the wrong attribute?
update_is18()
function update_is18() {
	if (age < 18) {
               document.getElementById('isunder18').selectedIndex = 0;
	}
	if (age >= 18) {
                document.getElementById('isunder18').selectedIndex = 1;
	}
 
}
 
Last Edit: 5 years 2 weeks ago by pscharfg. Reason: added code
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40157

  • mylo
  • mylo's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 13
  • Thank you received: 3
I'm assuming that the calculation script is triggering the update_is18 function? I'm wondering how the update_is18 function knows the value of 'age' because you didn't send it over.

I would expect your calculation script to store the number in a variable... we'll say it was 'age'. Then call the update_is18 function by passing that variable through to it like this...
update_is18(age);

Then the function would receive the value of 'age' like this...
function update_is18(age) {
...
}

But more to your question, you'll have to select the conditional field and toggle the display from 'none' to 'block' and back if the DOB field is changed.

For the record, I created a quick form to test and the following code works for me.
function calc18() {
  yDrop = document.getElementById('Birthdatey');
  y = yDrop.options[yDrop.selectedIndex].value;
  age = 2020 - y;
  if (age < 18) {
    document.getElementById('Under18').value = "True";
    document.querySelector('.rsform-block-parentname').style.display = "block";
  } else {
    document.getElementById('Under18').value = "False";
    document.querySelector('.rsform-block-parentname').style.display = "none";
  }
}
Last Edit: 5 years 2 weeks ago by mylo.
The administrator has disabled public write access.

Birthday field start and end year 5 years 2 weeks ago #40158

  • pscharfg
  • pscharfg's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Yes, I am passing 'age' to the function. I posted the relevant portion of the code (and call) - sorry for the confusion.

I didn't realize I needed to programmatically change the css. I thought the RSForm conditional field logic would handle that. I guess because the selection is triggered by a script, the form doesn't update the same way as if someone clicked the dropdown manually. I'm still confused as to why this is. Changing the css worked as you indicated though.
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!