jQuery validation additional method german date


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

An additional method for the jQuery validation plugin to validate a german date (dd.mm.YYYY) based on DateITA method.


Copy this code and paste it in your HTML
  1. jQuery.validator.addMethod(
  2. "dateDECH",
  3. function(value, element) {
  4. var check = false;
  5. var re = /^\d{1,2}\.\d{1,2}\.\d{4}$/;
  6. if( re.test(value)){
  7. var adata = value.split('.');
  8. var dd = parseInt(adata[0],10);
  9. var mm = parseInt(adata[1],10);
  10. var yyyy = parseInt(adata[2],10);
  11. var xdata = new Date(yyyy,mm-1,dd);
  12. if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == dd ) )
  13. check = true;
  14. else
  15. check = false;
  16. } else
  17. check = false;
  18. return this.optional(element) || check;
  19. },
  20. "Please enter a correct date"
  21. );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.