Date of Birth validation

Date Of Birth Validation.

File Name :

<html>
<head>
<title>Age calculator</title>
<script>

const date1 = new Date('01/06/2022');
const date2 = new Date('01/06/1987');
const diffTime = Math.abs(dob - todate);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");
alert(diffTime);
alert(diffDays);
if(diffDays > 12784 )
{
alert("You are not eligible");
}
else{
alert("You are Eligible");
}

</script>

<script>
function ageCalculator() {
var userinput = document.getElementById("DOB").value;
var dob = new Date(userinput);
if(userinput==null || userinput=='') {
document.getElementById("message").innerHTML = "**Choose a date please!";
return false;
} else {
var month_diff = Date.now() - dob.getTime();
var age_dt = new Date(month_diff);
var year = age_dt.getUTCFullYear();
var age = Math.abs(year - 1970);
alert(document.getElementById("result").innerHTML =
"Age is: " + age + " years. ")
}
}
</script>



<script>
const _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}

// test it
const a = new Date("2017-01-01"),
b = new Date("2017-07-25"),
difference = dateDiffInDays(a, b);
</script>

<script>
var date1 = new Date("7/11/2010");
var date2 = new Date("8/11/2010");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24), 10);

alert(diffDays )
</script>






</head>
<body>
<center>
<h2 style="color: 32A80F" align="center"> Calculate Age from Date of Birth <br> <br> </h2>
<b> Enter Date of Birth: <input type=date id = DOB> </b>
<span id = "message" style="color:red"> </span> <br><br>
<button type="submit" onclick = "ageCalculator()"> Calculate age </button> <br><br>
<h3 style="color:32A80F" id="result" align="center"></h3>
</center>
</body>
</html>

DOB Validation :-

File Name :

<select class="form-control" name="category" id="category">
<option value="" selected="selected" disabled="disabled">--- Please Select Community ---</option>
<option value="GEN" <?php if(set_value('category')=='GEN'){ echo 'selected'; } ?>>GEN</option>
<option value="OBC" <?php if(set_value('category')=='OBC'){ echo 'selected'; } ?>>OBC</option>
<option value="SC" <?php if(set_value('category')=='SC'){ echo 'selected'; } ?>>SC</option>
<option value="ST" <?php if(set_value('category')=='ST'){ echo 'selected'; } ?>>ST</option>
<option value="EWS" <?php if(set_value('category')=='EWS'){ echo 'selected'; } ?>>EWS</option>
</select>
<span class="error"><?php echo form_error('category'); ?></span>


<input type="date" class="form-control" name="dob" id="dob" value="<?php echo set_value('dob'); ?>" onblur="ValidateDOB()" placeholder=" Enter Date of Birth (dd/mm/yyyy)" autocomplete="off" style="background-color: #fff;" />


<script type="text/javascript">
function ValidateDOB() {

var cate = $('#category').val();
if(cate == 'OBC' || cate == 'SC' || cate == 'ST' || cate=='EWS') {

var lblError = document.getElementById("lblError");
//Get the date from the TextBox.
var dateinput = document.getElementById("dob").value;
var dob = new Date(dateinput);
if(dateinput==null || dateinput=='')
{
//document.getElementById("dob_error").innerHTML = "Please Enter Date of Birth";
// $("#dob_error").html("Enter Date of Birth");
return false;
}
else{
//calculate month difference from current date in time
var month_diff = Date.now() - dob.getTime();
//convert the calculated difference in date format
var age_dt = new Date(month_diff);
//extract year from date
var year = age_dt.getUTCFullYear();

//now calculate the age of the user
var age = Math.abs(year - 1970);
// alert(age);
lblError.innerHTML = "Date Of Birth is Not Eligible";
if(age >= 42)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}
else if(age <= 21)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}


lblError.innerHTML = "";

return true;

}


}
else{

var lblError = document.getElementById("lblError");
//Get the date from the TextBox.
var dateinput = document.getElementById("dob").value;
var dob = new Date(dateinput);
if(dateinput==null || dateinput=='')
{
//document.getElementById("dob_error").innerHTML = "Please Enter Date of Birth";
// $("#dob_error").html("Enter Date of Birth");
return false;
}
else{
//calculate month difference from current date in time
var month_diff = Date.now() - dob.getTime();
//convert the calculated difference in date format
var age_dt = new Date(month_diff);
//extract year from date
var year = age_dt.getUTCFullYear();

//now calculate the age of the user
var age = Math.abs(year - 1970);
// alert(age);
lblError.innerHTML = "Date Of Birth is Not Eligible";
if(age >= 39)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}
else if(age <= 21)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}


lblError.innerHTML = "";

return true;

}

}










/*
var parts = dob.split("/");

var dtDOB = new Date(parts[2] + "/" + parts[1] + "/" + parts[0]);
// alert(dtDOB);

var dtCurrent = new Date();

lblError.innerHTML = "Date Of Birth is Not Eligible";

if (dtCurrent.getFullYear() - dtDOB.getFullYear() > 50)
{
document.getElementById("dob").focus();
return false;
}

else if(dtCurrent.getFullYear() - dtDOB.getFullYear() < 21){
document.getElementById("dob").focus();
return false;
}

lblError.innerHTML = "";

return true;
*/


}
</script>

Calculate Age from Date of Birth :-

File Name :

<?php // echo changeDateFormat('d-m-Y',$row->dob);


$dob = new DateTime($row->dob);
$today = new DateTime('today');
// $today = new DateTime('2023-01-01 23:59:00');
$year = $dob->diff($today)->y;
$month = $dob->diff($today)->m;
$day = $dob->diff($today)->d;
// echo $year." year,"." ",$month." months,"." ".$day." days";
if(($year>=36 && $day>0))
{
?>
<p style='color:red;'> <?php echo changeDateFormat('d-m-Y',$row->dob); ?>&nbsp;&nbsp; <i class="fa fa-close" style="font-size:16px;color:red" aria-hidden="true"></i></p>



<?php }
else{ ?>
<p style='color:green;'> <?php echo changeDateFormat('d-m-Y',$row->dob); ?> &nbsp;&nbsp;<i class="fa fa-check" style="font-size:16px;color:green;" aria-hidden="true"></i></p>

<?php }
?>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here