Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi @Ankur Bawiskar 

 

Catalog client script-

find_real_file.png

 

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-

 

find_real_file.png

 

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.

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