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

 

// JOIN THE CONVERSATION

Share your experience

Bir sorunun, farklı bir çözümün veya eklemek istediğin bir notun var mı? Giriş yapmadan da yazabilirsin.

// KEEP READING

One more note.

All posts ↗︎