Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  172] [ 7]  / answers: 1 / hits: 16254  / 9 Years ago, tue, august 18, 2015, 12:00:00

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 or onSubmit Please go through the code.




function getAge(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;
}
return age;
}
var age = getAge(1987/08/31);
alert(age);
if (age < 18) {
alert(This age is restrict);

} else {

alert(This age is allowed);
}

<form>
<input type=text id=dob onBlur=function getAge(dateString) />
</form>




More From » php

 Answers
112

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()>


http://js.do/code/65643


[#65385] Friday, August 14, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;