开发者

Jquery form validation of custom date format DD-MM-YYYY

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 valida开发者_C百科te the field with a standard date format DD-MM-YYYY ??

thanks in advance.. :)

blasteralfred


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜