Search code examples
jqueryregexvalidationjquery-validation-engine

Add a custom regex validation in jQuery Validation Engine


I have problems adding a custom validation to validate fields entering time (with jQuery Validation Engine), in the following manner: hh:mm (e.g. 12:30);

As far as I know I should add the following

"timeFormat":{ 
    "regex":"/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/",
    "alertText":" errormessage in the locale chosen"}                           
}

in jquery.validationEngine-(the locale).js

and add the following in the input field class="validate[custom[timeFormat]]"

But for some reason it doesn't work, whatever I've entered it displays that the field is not valid. I suspect that my regex is something wrong, but I've tested it in http://gskinner.com/RegExr/ and it seems pretty OK.

Suggestions?


Solution

  • You don't need the quotes around your regex. Try this here:

    "timeFormat":{
        "regex": /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/,
        "alertText":" errormessage in the locale chosen"                           
    }