- 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-09-2022 05:30 AM
you can use gs.dateDiff() to return the diff in seconds and compare that to 15 years ago. If the diff is a negative number, the user picked a date in the future.
Change the date in the gdt2.setValue() to whatever you're using for input. E.g., gdt2.setValue(current.variables.birthday);
var gdt1 = GlideDate();
var gdt2 = GlideDate();
gdt2.setValue('2000-07-24');
var duration = gs.dateDiff(gdt2.getDisplayValue(), gdt1.getDisplayValue(), true);
fifteenYears = 15 * 365 * 24 * 60 * 60;
if (duration > fifteenYears) {
gs.info('No one is older than 15 years. Get real.');
}
else if (duration < 0) {
gs.info('Your birthday can\'t be in the future, ya goof.');
}
else {
gs.info('That\'s a nice lookin\' birthday you got there.');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 05:36 AM
leap year's can make 3 days/4 days missing
Anshu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 05:35 AM
ya goof!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 05:36 AM
Hi,
so basically the age should be more than 15 years
You can use onChange client script with GlideAjax
Script Include: It should be client callable
var calculateUtils = Class.create();
calculateUtils.prototype = Object.extendsObject(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'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('date_of_birth'); // give here proper variable name
var ga = new GlideAjax('calculateUtils');
ga.addParam('sysparm_name', "getDifference");
ga.addParam('sysparm_date', g_form.getValue('date_of_birth')); // give here proper variable name
ga.getXMLAnswer(function(answer){
if(answer.toString() == 'true'){
var message = 'Please provide a valid birth date';
g_form.clearValue('date_of_birth'); // give here proper variable name
g_form.showFieldMsg('date_of_birth', message, 'error', true);
}
});
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader