0
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 on the list he invalidates the form and does not send.
I managed to do just the opposite of what I wanted. You are only authorizing if you are not on the list, use this answer to assemble this script; https://stackoverflow.com/questions/37069086/validate-input-value-against-blacklist-array
Example in Fiddler https://jsfiddle.net/dorathoto/mfxn8mfj/2/
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 cannot notice.
It wouldn’t just change to $.inArray(value, Palavrasautorizadas) >= 0;
– Caique Romero
in theory it is, tried tbm as true, but change there and test every word, you will see that some work, others do not!
– Dorathoto
I think there’s some more detail missing, but I don’t know what it is.
– Dorathoto
In this fiddle inverti to 0, https://jsfiddle.net/mfxn8mfj/4/ can tell me a bug word so I can look for the problem.
– Caique Romero