Validate an age using jQuery Validator plug-in


/ Published in: jQuery
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. });

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.