Sunday, January 30, 2011

JavaScript - Date of Birth Validation

As a requirement for a project, I had to make sure that the applicant was at least 18 years of age. The code snippet below checks that the person is of age and (if he or she is underage) displays an alert pop-up. This code should be placed in the onSave event and will effectively prevent the CRM form from saving if the person is less than 18 years old:

if (crmForm.all.gov_birthdate.DataValue != null)  
           var maxDate = new Date(); //get today's date 
           maxDate.setFullYear(maxDate.getFullYear() - 18);

           if (maxDate < crmForm.all.gov_birthdate.DataValue)   //under 18  

                 alert('An applicant must be at least 18 years old.');
                 event.returnValue = false;
                 return false;



No comments:

Post a Comment