Hi! I am trying to check for illegal characters on submission to any input box and textarea, not just in the first or last. This is the code in fiddle. http://jsfiddle.net/19eggs/reQrL/4/
Your help is highly appreciated.
Please note: I don’t have control over how the html is written it is done automatically.
The problem is your selector is not selecting the correct input elements.
I would do it like this:
$('.formsubfree').click(function (event) {
var element,
value,
warningMessage = 'The characters & < > [ ] { } % are not allowed in any field.\
Please remove them and try again.',
pattern = /[%&<>\\[\\]{}]/;
if (this.value === "Save"){
element = $(this).prevAll(".forminfree");
} else {
element = $(this).parent().parent().prevAll("tr").find(".forminfree");
}
value = element.val();
if (pattern.test(value)) {
alert(warningMessage);
event.preventDefault();
}
});
Thank you very much for your reply and sorry to reply this late. I have done it slightly different but will incorporate your code somewhere else, I am working on. Thanks again and your code is much more better than mine.