Age Validation in jQuery

2014-09-21 • Snippets • Views: 1564

[php]

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body><form name="demoForm" id="demoForm" action="">
<input type="hidden" name="requiredAge" id="requiredAge" value="18">
<h4>Enter Birth Date</h4>
<p>Day: <input type="text" name="birthDay" id="birthDay" value=""></p>
<p>Month: <input type="text" name="birthMonth" id="birthMonth" value=""></p>
<p>Year: <input type="text" name="birthYear" id="birthYear" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>

<script language="JavaScript1.2">
$("#demoForm").submit(function(){
var day = $("#birthDay").val();
var month = $("#birthMonth").val();
var year = $("#birthYear").val();
var age = $("#requiredAge").val();
if (day == "" || month == "" || year == "") {
alert("You must enter all fields.");
return false;
}
var mydate = new Date();
mydate.setFullYear(year, month-1, day);

var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() – age);
if ((currdate – mydate) < 0){
alert("Sorry, only persons over the age of " + age + " may enter this site");
return false;
}
alert("Success! Submitting Form…");
return true;
});
</script>
</body>
</html>

[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Shares