You are viewing limited content. For full access, please sign in.

Question

Question

Dates limitations in a Collection

asked on April 30, 2017

Hi,

I have a date field into a collection. I added the following javascript :

//----------------------------------Date of Birth--------------------------------

var todaysDate = new Date(); // Gets today's date 
//Max date attribute is in "YYYY-MM-DD".  Need to format today's date accordingly 
var dob_year = todaysDate.getFullYear()-18; 
//var dob_year = todaysDate.getFullYear();                        // YYYY 
var dob_month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);  // MM 
var dob_day = ("0" + todaysDate.getDate()).slice(-2);           // DD 
var dob_maxDate = (dob_year +"-"+ dob_month +"-"+ dob_day); // Results in "YYYY-MM-DD" for today's date  
// Now to set the max date value for the calendar to be today's date 
$('.dateofbirth input').attr('max',dob_maxDate);

I also added the class dateofbirth to the field. 

This code add a restriction to the selection of dates. So people who have less than 18 years old cannot insert a date.

This work well on its on, but when the field is found into a collection, when i click on add, the code don't work on the replicated version of date field.

Please advise,

Best regards,

-Guillaume

0 0

Replies

replied on April 30, 2017

This code, is only being run on the form when it initially loads, and would only impact fields that already exist on the form.  When you add rows to a table or collection you are basically adding entirely new fields to the form.  So you'd need to re-run this code at that time as well.

I recommend placing your code inside a function (     function myFunctionName() { ... }     ) and then you can call that code whenever you need.  So you can call it when the form loads and also when your collection is changed.  (     $('.myCollectionName').on('change', function() {  myFunctionName;  });     ).

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.