I want to validate a form with Jquery + JQuery Validation, using a list of authorized words.
Ex: var PalavrasAutorizadas = ['foo', 'bar', 'fizz', 'buzz' ];
If he enters any word that is not in the list he invalidates the form and does not send it.
I was able to do exactly the opposite of what I wanted. You are only authorizing if it is not in the list, use this response to mount this script; link
Example in fiddler link
Javascript code:
var PalavrasAutorizadas = ['foo', 'bar', 'fizz', 'buzz' ];
jQuery.validator.addMethod("word", function(value) {
return $.inArray(value, PalavrasAutorizadas) == -1;
}, 'a palavra não consta na lista');
$("#submit").validate({
//errorPlacement: function(error, element) {},
rules: {
word: {
required: true
}
},
submitHandler: function(form) {
alert('Submitted');
}
});
UPDATE : I have tried to change the return of the array to 1 or true, but when testing each word does not work for all words, I think there is some detail in this function that I can not understand.