I am using Asp.Net MVC 3 and I am adding a validator later in the lifecycle through jquery with jquery.validate using [this code][1]:
Thanks to the answer of redsquare I added a method like this:
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var check = false;
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
now all you need to do to validate against any regex is this:
$("Textbox").rules("add", { regex: "^[a-zA-Z'.\s]{1,40}$" })
The validation is working fine, but the actual message isn’t showing up anywhere… what am I doing wrong? Thank you.
I had the same issue. Never found what was causing the issue and I’ve ended up using MVC unobtrusive validation by assigning validation attributes to my input fields :
so assuming you’ve got a #Textbox input :
Easy way to turn on the validation is just to assign data-val-* attributes :
This is they way I do it on my website and it works. The way you call your validation may collide with they way how Microsoft implemented jQuery validator, however I couldn’t find where that is