I need to validate if the age for a alcohol website. And what I need is all here. I\'m nearly there but I\'m not sure is correct . Locally doesn\'t work. I need the
$('.submit').click(function() {
should be
$('#submit').click(function() {
$(".submit") refers to a CLASS and $("#submit") refers to an ID.
and you'll have to add some logic for checking if the remember checkbox is checked, i think you attempted to, but weren't able to see if it was successful because the code never executed. I added the logic for you (simple if statement) and within that, you need to add the cookie creation code.
if ($('#remember').is(":checked")) {
$.cookie('age', age, { expires: 365 });
} else {
$.cookie('age', age);
}
(uses https://github.com/carhartl/jquery-cookie)
http://jsfiddle.net/bhdry/45/
As for the Age Validation piece, try this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script src="http://cloud.github.com/downloads/digitalBush/jquery.maskedinput/jquery.maskedinput-1.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(function() {
$("#frm-verify-age").submit(function(){
var age = 18;
var mydate = new Date($("#bday").val());
mydate.setFullYear(mydate.getFullYear());
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;
}
return true;
});
$("#bday").datepicker({
buttonText: 'Choose a Date'
});
$("#bday").mask("99/99/9999");
});
});
</script>
</head>
<body>
<form name="frm-verify-age" id="frm-verify-age">
Birthdate <input style="background-color:#FFFFa0" type="text" id="bday" name="bday" size="12" />
<input name="frm-verify-submit" id="frm-verify-submit" type="submit" value="submit" />
</form>
</body>
</html>
For the Cookie Saving: https://github.com/carhartl/jquery-cookie
Use http://timeago.yarp.com plugin.
let them type in their date of birth and then you can use
jQuery.timeago("2008-07-17"); //=> "3 years ago"
if that number >= 21 they are of age
for the cookie you can use this plugin https://github.com/carhartl/jquery-cookie
$.cookie('the_cookie', 'the_value'); //set cookie
$.cookie('the_cookie'); // read cookie
SECOND EDIT: full source http://jsfiddle.net/bhdry/
$('.submit').click(function() {
should be $('#submit').click(function() {
although there is some weird code that I don't understand and causes an error
$('input[type="checkbox"]:checked').length > 0();
Well, since you are going to have three input fields instead of one, the task is just to apply the mask to three textboxes instead of to one, and then constructing the argument to the timeago
method call. The rest would be unchanged. Something like this:
$('input[name="DAY"]').mask("99");
$('input[name="MONTH"]').mask("99");
$('input[name="YEAR"]').mask("9999");
$('.deleteCookie').click(function() {
$.cookie('age', null);
});
$('.submit').click(function() {
var enteredDOB = $('input[name="DAY"]').val() + "/" +
$('input[name="MONTH"]').val() + "/" +
$('input[name="YEAR"]').val();
var age = jQuery.timeago(enteredDOB);
if (age === 'NaN years ago') {
alert('DOB must be valid');
} else {
$.cookie('age', age);
if (parseInt(age, 10) >= 21) {
alert('you\'re of age');
} else {
alert('you\'re too young');
}
}
});
if (null !== $.cookie('age')) {
alert('saved Cookie ' + $.cookie('age'));
}
With regards to the "remember me" functionality, it is just a question of reading the age cookie upon page load, and redirecting to the next page if the age cookie contains a valid value, although this would probably be a check better done server-side.
Get difference between 2 dates in javascript?
// parse the users input into a date
var date1 = new Date("7/11/2010"); //birthday
// get today's date
var date2 = new Date();
// legal age
var age = 18;
// get the difference in milliseconds since Epoch
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
// convert it to days
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
// there are 365.242199 days in a year * years, this is the minimum # of days old the person must be. return true or false if they are old enough.
return (diffDays > (age * 365.242199));