I need to create a custom rule for the Jquery Validation Plugin that checks for one specific word.
In this case, the user must answer a question correctly by entering the word “cat” into the input field. The custom rule would need to define the answer as “cat”, I was thinking as a variable, and then check for it.
My failed attempt:
jQuery.validator.addMethod("answercheck", function(value, element) {
var password = string('hot');
return value(password);
}, "Type the correct answer");
and the setup:
rules: {
input_answer{
required: true,
answercheck: true
}
messages: {
input_answer{
required: "Answer the question",
answercheck: "Your answer is incorrect"
}
Any ideas?
As others have stated, this is not a secure method since anyone could see/manipulate the JavaScript and easily bypass it.
However, to simply answer your question, here’s how it’s done. (It’s also a good learning exercise about using
addMethod)…And you also wouldn’t need to specify a message for
answercheckbelow, because it’s already defined above within themethoditself. (You were also missing some braces{, colons:, and a comma,in this code.)Working jsFiddle DEMO:
http://jsfiddle.net/ht8Lu/
As per the documentation, you’ll need to construct your own custom method.