SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    The problem is your selector is not selecting the correct input elements.

    I would do it like this:

    Code JavaScript:
    $('.formsubfree').click(function (event) {
        var element,
            value,
            warningMessage = 'The characters & < > [ ] { } % are not allowed in any field.\n 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.

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Quote Originally Posted by Pullo View Post
    Hi there,

    The problem is your selector is not selecting the correct input elements.

    I would do it like this:

    Code JavaScript:
    $('.formsubfree').click(function (event) {
        var element,
            value,
            warningMessage = 'The characters & < > [ ] { } % are not allowed in any field.\n 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.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    No probs

    Thanks for taking the time to report back.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •