Reputation: 275
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.
Upvotes: 1
Views: 1746
Reputation: 630379
When using RegExp()
you should leave off the /
and flags, it should just be:
$("#article_text").rules("add", { regex: "test phrase" });
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:
$.validator.addMethod("regex", function(value, element, regexp) {
return this.optional(element) || regexp.test(value);
}, "Please include your keyword(s).");
Then you'd call it like this:
$("#article_text").rules("add", { regex: /test phrase/i });
You can test that version out here.
Upvotes: 2