// JavaScript

Under-18 Date Check

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));
}

 

// FEEDBACK

Was this useful?

Your rating shows how helpful this note is to other readers.

Was this useful?

Click the stars to rate.

Average rating 0 / 5. Total votes: 0

No one has rated this post yet. Be the first!

Sorry this post was not helpful.

Your feedback is valuable to us.

How can we make this post more helpful?

// JOIN THE CONVERSATION

Share your experience

Got a question, a different solution or a note to add?

// KEEP READING

One more note.

All posts ↗︎