I need to validate a textarea using the plugin jQuery Validate, to do that I use a regex and I add a method to the plugin:
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var check = false;
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"No special Characters allowed here. Use only upper and lowercase letters (A through Z; a through z), numbers and punctuation marks (. , : ; ? ' ' \" - = ~ ! @ # $ % ^ & * ( ) _ + / < > { } )"
);
Then in the options I add the regex:
comments:{
required: true,
maxlength: 8000,
regex: /[^A-Za-z\d\-\=\~\!@#\%&\*\(\)_\+\\\/<>\?\{\}\.\$‘\^\+\"\';:,\s]/
}
This "works" in a certain way, it does detect the invalid characters and displays the message, the problem is it only works when the special characters are the only ones in the box, for instance:
| `` ° ¬ // This shows the error message but...
test | // This won't show the message
So if one allowed character is there then the validation just stops working. Am I missing something?
P.S. I'm pretty sure this has something to do with the plugin 'cause I've tested the regex with just javascript and it works well.