I want the validation for date of birth. I have used new Date method so that i should not get date after today. But even though i insert date after today it doesn't show invalid date.
var pattern = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/
var date= new Date();
if (dateofbirth == "" || dateofbirth == null||!pattern.test(dateofbirth)) {
alert("invalid date of birth should in yyyy-mm-dd");
return false;
}
else if(dateofbirth >date){
alert("invalid date");
return false;
}
else{
alert("valid date");
}
You can you my code :
var validation = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/
var date= new Date();
if (dateofbirth == "" || dateofbirth == null||!validation.test(dateofbirth)) {
alert("Date of Birth is Invalid it should in yyyy-mm-dd");
return false;
}
else if(dateofbirth >date.getFullYear()){
alert("Invaid Date");
return false;
}
else{
alert("Your Date is Valid");
}
var pattern = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/
var date= new Date();
if (dateofbirth == "" || dateofbirth == null||!pattern.test(dateofbirth)) {
alert("invalid date of birth should in yyyy-mm-dd");
return false;
}
else if(dateofbirth >date.getFullYear()){
// get full year give current year you can compare with that year
// to date of birth
alert("invalid date");
return false;
}
else{
alert("valid date");
}
dateofbirth
is not defined. Try to add an input.