X

Jquery Validate Plugin : Custom date format validation (mm/dd/yyyy)

Suppose, you are using Jquery Validator plugin to validate form fields and now you want to validate date field with custom format like mm/dd/yyyy or dd/mm/yyyy then there is no way to do by passing parameters.

Currently, Jquery Validator plugin is not allowing to define date format for validation.

In this case, you need to add one custom function and you need to write date format validation code in it and need to pass that function as parameter for that field in validate rule function.

See Example below :

jQuery.validator.addMethod(
        "trioDate",
        function(value, element) {
            return value.match(/^(0[1-9]|1[012])[\/|-](0[1-9]|[12][0-9]|3[01])[\/|-](19\d\d|2\d\d\d)$/);
        },
        "Please enter a date in the format mm/dd/yyyy"
    );

///////

$("#yourform").validate({
            rules: {
                dob: {
                    required : true,
                    date:true,
                    trioDate:true
                },
            }
}}

You can change regular expression according to your requirement.

This post was last modified on January 6, 2012 1:33 pm

Categories: Jquery
Chetan Patel: Chetan Patel is blogger, writes on Tech News, current trends, Gadgets Reviews, Website owner guide and Tips. You can connect him at <a href="https://plus.google.com/118385579556796801405?rel=author">Google+</a> & <a href="https://www.facebook.com/patecj">Facebook</a>.