Scenario: In Basic/Entity Form On change of Date of Birth Field, If Date of Birth is Less then 15 Years Through an Alert that the Cadet Age should be greater then 15 Years.
Step 1: Get the Schema Name of Date of Birth Field.
Step 2: Write jQuery as below.
$(document).ready(function () {
var dpcontrol = $('div.control')[2];
$(dpcontrol).on("dp.change", function (e) {
caliculateCadetDOB();
});
});
function caliculateCadetDOB() {
var cadetDOB = $("#new_dateofbirth").val();
var cadetDOBInDateFormat = new Date(cadetDOB);
var day = cadetDOBInDateFormat.getDate();
var month = cadetDOBInDateFormat.getMonth();
var year = cadetDOBInDateFormat.getFullYear()
var day, month, year;
var dobstring = month + 1 + "/" + day + "/" + year;
//var dobstring = month + "/" + day + "/" + year;
var dob = new Date(dobstring);
var cadetAge = getAge(dob);
if (cadetAge < 15) {
alert("Cadet Age should be greater then 15 Years");
$("#new_dateofbirth").datepicker({ dateFormat: 'dd,MM,yyyy' }).val(''); // Clear date
$('input[aria-describedby="new_dateofbirth_description"]').val("");// Clear date display
$("#new_dateofbirth").focus();
}
}
function getAge(DOB) {
var today = new Date();
var birthDate = new Date(DOB);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
var d = today.getDate() - birthDate.getDate();
if (d == 0 && m == 0) {
}
else if (d == 0) {
if (m > 0) {
}
else {
age--;
}
}
else if (d < 0) {
if (m > 0) {
}
else {
age--;
}
}
else if (m >= 0 && d > 0) {
}
else {
age--;
}
return age;
}
Step 3: Paste the code in respective Basic / Entity Form and save the records shown in below image.
Step 4: Clear the Portal Server Side Cache.
Step 5: Now Check your functionality is working as expected or not.
Hence, In Both cases it has been working as expected through jQuery.
Hope it helps 😜😜😜 to you guys, if you struck anywhere add your comments in comment box section will help you out to fix the issue.