0

I am using jquery-validate plugin to validate my form. I has validation rule like

var validator = $("#myform").validate({
rules: {
            Date: {
                required: true,,
          },
       },
});

How can I validate the field with a standard date format DD-MM-YYYY ??

thanks in advance.. :)

blasteralfred

Alfred
  • 21,058
  • 61
  • 167
  • 249
  • possible duplicate of [Custom date format with jQuery validation plugin](http://stackoverflow.com/questions/511439/custom-date-format-with-jquery-validation-plugin) – Haim Evgi Feb 22 '11 at 12:23
  • I already made a walk through that post, but i found it buggy. I made a comment there too... – Alfred Feb 22 '11 at 13:39

1 Answers1

0

You should be able to use the 'custom method' in the plugin. You basically call your own JS function which will parse the date (using whatever you want, probably a regexp and a JS date check) and then decide if your date passes/fails, by returning a true/false, or a string of either null for a pass or an error message for a fail. See the 'Validate' plugin for examples.

To check the format of the text string-date, try a regexp such as:

var vDT="(([012]?[1-9])|10|20|30|31)[/](([0]?[1-9])|10|11|12)[/](19|20)?[0-9]{2}";
var regex = new RegExp(vDT);
return (regex.test(dateString));

The regexp will check for a 'good' date, but not a perfect date, eg: 99/99/9999 fails, 31/03/2011 passes, but so does 31/02/2011. Note this accepts 2 digit or 19xx or 20xx years, but you may want to limit this to 4 digits only as you might find yourself back in the 20 th century.

To create a JS compatible US date from a UK date, you need to do something like:

var uk = '21/11/2011';
var uka = split(uk, '/');
var jsdate = new date(uka[2], uka[1]-1, uka[0])

Then check your jsdate is valid...

andora
  • 1,326
  • 1
  • 13
  • 23