In ServiceNow, you can implement an 18-year age check for a date field as soon as the birth date information is entered by writing the following in the field’s onChange section in the client script area.
Note: It is assumed that the field’s date value is passed to the function in YYYY-MM-DD format.
var g = newValue;
var yil,ay,gun;
yil = g.substr(0, 4).toString();
ay = g.substr(5, 2).toString();
gun = g.substr(8, 2).toString();
var d = new Date( yil, ay, gun );
// alert( calcAge(d) );
if( calcAge(d) < 18 ){
g_form.clearValue('u_date_of_birth');
alert('Your age is under 18.');
}
}
function calcAge(dateString) {
var birthday = +new Date(dateString);
return ~~((Date.now() - birthday) / (31557600000));
}

Share your experience
Got a question, a different solution or a note to add?
Log in or join us to write a comment.
Log in ↗︎Sign up ↗︎