How do I verify age using jQuery?

I need a simple age verification script.  I have the basics for it.

$('#frm-verify-age').submit(function() {
                  var
                        min_age = 18;
                        month = $('#frm-verify-month').val();
                        day = $('#frm-verify-day').val();
                        year = $('#frm-verify-year').val();
                        var theirDate = new Date((year + min_age), month, day);
                        var today = new Date;
                        if ( (today.getTime() - theirDate.getTime()) < 0) {
                              alert("You are too young to enter this site!");
                        }
                        else {
                              alert("You are old enough");
                        }
                        return false;
            });

<form action="#" method="post" name="frm-verify-age" id="frm-verify-age">
                        <fieldset class="first">
                              <p class="month">
                                    <label>Month</label>
                                    <input name="frm-verify-month" id="frm-verify-month" type="text" value="MM" />
                              </p>
                              <p class="day">
                                    <label>Day</label>
                                    <input name="frm-verify-day" id="frm-verify-day" type="text" value="DD" />
                              </p>
                              <p class="year last">
                                    <label>Year</label>
                                    <input name="frm-verify-year" id="frm-verify-year" type="text" value="YYYY" />
                              </p>
                        </fieldset>
                        <fieldset>
                              <p class="state last">
                                    <label>Year</label>
                                    <input name="frm-verify-state" id="frm-verify-state" type="text" value="Select" />
                              </p>
                        </fieldset>
                        <fieldset class="last">
                              <p class="submit">
                                    <label>Year</label>
                                    <input name="frm-verify-submit" id="frm-verify-submit" type="submit" value="" />
                              </p>
                        </fieldset>
                  </form>
aznprncriticAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
Integration :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="jquery.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$('#frm-verify-age').submit(function() {
			var min_age = 18;
			month = $('#frm-verify-month').val();
			day = $('#frm-verify-day').val();
			year = $('#frm-verify-year').val();
			var theirDate = new Date(parseInt(year) + min_age, month-1, day);
			var today = new Date();
			if ( (today.getTime() - theirDate.getTime()) >= 0) {
				alert("You are old enough");
				return true;
			}
			alert("You are too young to enter this site!");
			return false;
		 });
	});
</script>
</head>
<body>
<form action="#" method="post" name="frm-verify-age" id="frm-verify-age">
    <fieldset class="first">
    <p class="month">
        <label>Month</label>
        <input name="frm-verify-month" id="frm-verify-month" type="text" value="MM" />
    </p>
    <p class="day">
        <label>Day</label>
        <input name="frm-verify-day" id="frm-verify-day" type="text" value="DD" />
    </p>
    <p class="year last">
        <label>Year</label>
        <input name="frm-verify-year" id="frm-verify-year" type="text" value="YYYY" />
    </p>
    </fieldset>
    <fieldset>
    <p class="state last">
        <label>Year</label>
        <input name="frm-verify-state" id="frm-verify-state" type="text" value="Select" />
    </p>
    </fieldset>
    <fieldset class="last">
        <p class="submit">
        <label>Year</label>
    <input name="frm-verify-submit" id="frm-verify-submit" type="submit" value="valid" />
    </p>
    </fieldset>
</form>
</body>
</html>

Open in new window

0
 
mstrelanCommented:
does it work?
0
 
leakim971PluritechnicianCommented:
Use :


<script language="javascript">
	$(document).ready(function() {
		$('#frm-verify-age').submit(function() {
			var min_age = 18;
			month = $('#frm-verify-month').val();
			day = $('#frm-verify-day').val();
			year = $('#frm-verify-year').val();
			var theirDate = new Date(parseInt(year) + min_age, month-1, day);
			var today = new Date();
			if ( (today.getTime() - theirDate.getTime()) >= 0) {
				alert("You are old enough");
				return true;
			}
			alert("You are too young to enter this site!");
			return false;
		 });
	});
</script>

Open in new window

0
 
aznprncriticAuthor Commented:
nice, was missing the -1 for month
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.