Validation on Birth Date

Rekha Tiwari
Kilo Guru

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 

so if the user puts a date that will make it 14 years and 364 days it will give an message "Please provide a valid birth date"
 
Can you please help me with this?
 
1 ACCEPTED SOLUTION

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

_ChrisHelming
Tera Guru

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

leap year's can make 3 days/4 days missing

 

 

Regards,
Anshu

Scotty Williams
Tera Contributor

ya goof!

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader