This topic is locked
[SOLVED]

 Javascript validate start dates and end dates against each other

3/24/2020 7:50:01 PM
PHPRunner General questions
woodey2002 author

Hi Guys,
I just noticed a shortcoming in some Javascript that I wrote for my project.
On an add page, I use Javascript to validate start dates and end dates against each other. I use Javascript to validate 9 various dates against each other throughout the add page.
For example Date of birth must be less than Date of admission and Date of admission must be greater than Date of birth and so on...
Here is an example of the code I use to achieve this single check:

// DOB Vs DOA validate date START...
var dob = Runner.getControl(pageid, 'date_Of_Birth');

var doa = Runner.getControl(pageid, 'date_Of_Admission');
doa.on('change', function(e) {
if (doa.getValue()< dob.getValue() != !doa.getValue() ){

doa.setInvalid( 'Date of Admission must be greater than Date of birth' );

doa.setValue('');

}

else

doa.setValid();

});
dob.on('change', function(e) {
if (dob.getValue()> doa.getValue() != !doa.getValue() ){

dob.setInvalid( 'Date of birth must be less than Date of Admission' );

dob.setValue('');

}

else

dob.setValid();

}

);

// DOB Vs DOA validate date END...


This code works really well if validating a pair of unique singular date fields however it seems to run into trouble when I reference a date that I have already used to validate in a previous set of JS date validations. For example Date of referral must be greater than Date of admission and Date of admission must be less than Date of referral. (Notice that Date of admission was used in the previous snippet)

// DOR Vs DOA validate date START...

var dor = Runner.getControl(pageid, 'date_Of_Referral');

var doa1 = Runner.getControl(pageid, 'date_Of_Admission');
dor.on('change', function(e) {
if (dor.getValue()< doa1.getValue() != !dor.getValue() ){

dor.setInvalid( 'Date of Referral must be greater than Date of admission' );

dor.setValue('');

}

else

dor.setValid();

});
doa1.on('change', function(e) {
if (doa1.getValue()> dor.getValue() != !dor.getValue() ){

doa1.setInvalid( 'Date of Admission must be less than Date of Referral' );

doa1.setValue('');

}

else

doa1.setValid();

});

// DOR Vs DOA validate date END...


So the problem seems to be I think when I reference the date_Of_Admission field having previously used it in the earlier start and end date validation code.
This field seems to ask to be validated against a date field with no value yet so therefore blank (the field wrongly asked to be validated against is always the last date field in the group of 9 dates i'm working with)
Any ideas how I can tweak either of the above snippets problems to sort this out?
Many thanks,

J

woodey2002 author 3/30/2020

Hi Guys,
I managed to figure it out...
I fixed it by adding .off('change') to each .on('change') function that was causing me trouble and that seemed to sort it.
James