Alert for illegal characters int input and text area

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.

Hi there,

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();
    }
});

Here’s a fiddle.

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.

No probs :slight_smile:

Thanks for taking the time to report back.