I’m trying to check a textarea to make sure it contains text phrase(s).
This is what I have so far but it’s not working:
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var check = false;
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please include your keyword(s)."
);
$("#article_text").rules("add", { regex: "/test phrase/i" });
Any ideas? Ideally, i’d like it to check for multiple phrases and display the error if any one of them isn’t included in the textarea.
When using
RegExp()you should leave off the/and flags, it should just be:You can test it here. But since you want flags, a better way is to just use it directly, not creating from a string, like this:
Then you’d call it like this:
You can test that version out here.