Date Field Validation - Unhandled Exception

I have some code verifying whether a patient is a minor or not. To do this, I compare the date of birth to today's date minus 18 years. That is all working.

The issue I am seeing is if the date is not entered in a valid format (i.e. they enter 2/15/81 vs 2/15/1981), the date field value is null and the date calculations don't work. I found this thread (Date format validation) and tried to add a date validator. When I have the date validator and put in a valid date (e.g. 2/15/1981), the date field validator is called. When I enter an invalid date (e.g. 2/15/81), the date field validator is never called. The developer windows shows "Uncaught TypeError: Cannot read properties of null (reading 'getFullYear')"

How do you validate a date field value if the date field validator is not trigger on an invalid date field value?

My date field validator code is as follows:

fd.field('PatDOB').addValidator({
    name: 'PatDOB Validation',
    error: '',
    validate:function(value){
        if (fd.field('PatDOB').value == null) {
            this.error = 'Please specify a valid patient date of birth';
            return false;
        }
        return true;
    }
});

Full error message is:

32.c6b8d25aa74b5594b561.js:1 Uncaught TypeError: Cannot read properties of null (reading 'getFullYear')
at init.change (32.c6b8d25aa74b5594b561.js:1)
at init.trigger (2.4b8c887932910e7901e2.js:1)
at init._change (4.41c2a6a3ce3efe95d14d.js:1)
at init._blur (4.41c2a6a3ce3efe95d14d.js:1)
at HTMLInputElement.o (app.js:39)
at HTMLInputElement.dispatch (app.js:39)
at HTMLInputElement.v.handle (app.js:39)
at Object.trigger (app.js:39)
at Object.simulate (app.js:39)
at HTMLDocument.n (app.js:39)

Dear @kfarren,
The issue and the error are not from the validator code, they are from the other calculation you're doing - can you share the rest of the code that uses this fd.field('PatDOB') with us?

The only other reference I have to the field PatDOB is in the submit validator (which isn't called when the error is seen).

We ask for information on whether the insurance guarantor is the patient or a parent/guardian. This code determines whether the insurance guarantor is at least 18 years old. It calculates an eligible date (18 years from today's date). It then determines whether it should compare the DOB for the patient or the legal guardian (depending on the input field of who is the insurance guarantor)

		//Verify that the insurance guarantor is at least 18 years old
		var GuarantorDOB;
		var GuarantorName;
		
		var EligibleDate;
		var CurrentDate = new Date();
		console.log (CurrentDate);
		EligibleDate = CurrentDate.setFullYear( CurrentDate.getFullYear() - 18 );
		console.log (EligibleDate);
		
		//Save guarantor DOB for parent/guardian and patient DOB otherwise
		if (fd.field('RespParty').value == "Parent/Guardian") {
	
		    GuarantorDOB = fd.field('GuardDOB').value;
		    GuarantorName = fd.field('PatGuard').value;
	
	    } else {
		
		    GuarantorDOB = fd.field('PatDOB').value;
		    GuarantorName = fd.field('PatName').value;
	
	    }

If I need to export and upload the entire form, I can. Please let me know.

Thanks,

Kevin

I removed all the code that I had added and was still getting that error. At that point, the code wasn't doing any calls to getFullYear, but the error was still present. I cleared my browser data and the error went away.

Thanks for your help. For some reason the browser had stored a previous version of the code and was not seeing my updates.

I will add in checks to ensure the date fields aren't null before calling these date functions. That will avoid the error.

You can consider this closed.

Kevin

1 Like