Posted By

1man on 10/28/10


Tagged

check jquery validator AGE


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Tyster
jillella
gotoru


Validate an age using jQuery Validator plug-in


 / Published in: jQuery
 

URL: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Useful snippet i found on a forum, use it to check to see if a user is old enough to submit a form.

  1. //Legal age validator check
  2. $.validator.addMethod('check_date_of_birth', function(value, element) {
  3. var day = $("#dob_day").val();
  4. var month = $("#dob_month").val();
  5. var year = $("#dob_year").val();
  6. var age = 18;
  7.  
  8. var mydate = new Date();
  9. mydate.setFullYear(year, month-1, day);
  10.  
  11. var currdate = new Date();
  12. currdate.setFullYear(currdate.getFullYear() - age);
  13.  
  14. return (currdate - mydate < 0 ? false : true);
  15. });
  16.  
  17. //Usage
  18. $("#theform").validate({
  19. rules: {
  20. dob_day: {
  21. check_date_of_birth: true,
  22. required: true
  23. }
  24. }
  25. });

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: GMali on September 11, 2012

You can change the last line of the function to this:

return currdate < mydate;

You need to login to post a comment.