I Need To Restrict Age For Below 18 Years Age From The Current Date In Php
I need to restrict age for below 18 years of age from the current date in Php using javascript or ajax. How can I do this? Please check my code I want to calculate the age onblur o
Solution 1:
Try this..
<script>
function getAge() {
var dateString = document.getElementById("date").value;
if(dateString !="")
{
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
var da = today.getDate() - birthDate.getDate();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
if(m<0){
m +=12;
}
if(da<0){
da +=30;
}
if(age < 18 || age > 100)
{
alert("Age "+age+" is restrict");
} else {
alert("Age "+age+" is allowed");
}
} else {
alert("please provide your date of birth");
}
}
</script>
<input type="text" id="date" value="1987/08/31" onblur="getAge()">
Solution 2:
Here Another Example Of Age Validation Please Try It
function getAge()
{
var dateString = document.getElementById("date").value;
alert(dateString);
var today = new Date();
var year = today.getFullYear()-18;
alert(year);
var month=0;
if(today.getMonth() < 10)
{
month="0"+(today.getMonth()+1);
}
else
{
month=(today.getMonth()+1);
}
alert(month);
var day=0;
if(today.getDate() < 10)
{
day="0"+today.getDate();
}
else
{
day=today.getDate();
}
alert(day);
var str = year +"-"+ month +"-"+ day;
var date1 = new Date();
date1 = str;
alert(str);
if (dateString>str)
{
alert("You Are NOt Able To Join Us!");
}
else
{
alert("You Are Able To Join Us!");
}
}
</script>
<input type="date" id="date" />
<input type="submit" id="btnSubmit" value="Submit" class="btn" name="B4" onclick="return getAge();">
Solution 3:
Here's another example of how you could get this to work which might be cleaner!
function getAge()
{
var dateString = document.getElementById("dob").value;
if(dateString !="")
{
var today = new Date();
var birthDate = new Date(dateString); //format is mm.dd.yyyy
var age = today.getFullYear() - birthDate.getFullYear();
if(age < 18 || age > 100)
{
alert("Age "+age+" is restrict");
}
else
{
alert("Age "+age+" is allowed");
}
}
else
{
alert("please provide your date of birth");
}
}
<body>
<input type="date" id="dob">
<br>
<input type="button" value="Get Age" onclick="return getAge()">
</body>
Solution 4:
$date = DateTime::createFromFormat('d/m/Y', $this->input->post('age'));
$dob = $date->format('Y-m-d');
$birthdate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthdate->diff($today)->y;
if ($age >= 18) { true } else { false }
Post a Comment for "I Need To Restrict Age For Below 18 Years Age From The Current Date In Php"