- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 04:33 AM
Hi All,
I need to put validation on Birth Date field in a way that user should only allow past dates and 15 years ago from current date
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 07:17 PM
Hi,
are you in scoped app?
dateDiff() won't work in scoped app
update as this
getDifference: function(){
var date = new GlideDateTime(this.getParameter('sysparm_date'));
var nowTime = new GlideDateTime();
var dur = new GlideDuration();
dur = GlideDateTime.subtract(date, nowTime);
var years = dur.getNumericValue()/31556952000; // 1 year is 31556952000 milliseconds
if(parseInt(years) < 15){
return true;
}
else{
return false;
}
},
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 07:28 AM
Hi
Catalog client script-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
g_form.hideFieldMsg('birth_date'); // give here proper variable name
var ga = new GlideAjax('sn_hr_core.calculateUtils');
ga.addParam('sysparm_name', "getDifference");
ga.addParam('sysparm_date', g_form.getValue('birth_date')); // give here proper variable name
ga.getXML(getDate);
function getDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (answer.toString() == 'true') {
alert('1');
var message = 'Please provide a valid birth date';
g_form.clearValue('birth_date'); // give here proper variable name
g_form.showFieldMsg('birth_date', message, 'error', true);
}
}
}
SI-
var calculateUtils = Class.create();
calculateUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDifference: function() {
var date = new GlideDateTime(this.getParameter('sysparm_date'));
var nowTime = new GlideDateTime();
var diff = gs.dateDiff(date, nowTime, true);
var years = diff / 31556952000; // 1 year is 31556952000 milliseconds
if (parseInt(years) < 15) {
return true;
} else {
return false;
}
},
type: 'calculateUtils '
});
It is not working. Alert is coming as null
Please suugest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 07:17 PM
Hi,
are you in scoped app?
dateDiff() won't work in scoped app
update as this
getDifference: function(){
var date = new GlideDateTime(this.getParameter('sysparm_date'));
var nowTime = new GlideDateTime();
var dur = new GlideDuration();
dur = GlideDateTime.subtract(date, nowTime);
var years = dur.getNumericValue()/31556952000; // 1 year is 31556952000 milliseconds
if(parseInt(years) < 15){
return true;
}
else{
return false;
}
},
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader