Validating a Date Format


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

This is used to validate a date in a specific format. The code example is to validate whether it is a valid dd/MM/yyyy Date Format.


Copy this code and paste it in your HTML
  1. function ValidateDateFormat(input) {
  2. var dateString = input.value;
  3.  
  4. var regex = /(((0[1-9]|1[0-2])\/(0|1)[0-9]|2[0-9]|3[0-1])\/((19|20)\d\d))$/;
  5.  
  6. //Check whether valid dd/MM/yyyy Date Format.
  7. if (regex.test(dateString) || dateString.length == 0) {
  8. ShowHideError("none");
  9. } else {
  10. ShowHideError("block");
  11. input.focus();
  12. input.select();
  13. }
  14. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.