Checking for illegal characters in a textbox (eg. <,>,!,@,#,$,%,^,&,*,(,) )

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uicouic
    New Member
    • Jan 2009
    • 24

    Checking for illegal characters in a textbox (eg. <,>,!,@,#,$,%,^,&,*,(,) )

    I have a textbox named "txtName" and a button (btnSave) on a webform.
    After I have typed the illegal characters into the textbox and click "Save", I would like the webform to check for those characters and display a message, "Illegal Characters not allowed". How do I achieve that in JavaScript?

    Sorry if it seems too simple as I am kinda weak in programming.

    Thanks.
  • Uncle Dickie
    New Member
    • Nov 2008
    • 67

    #2
    You can use the indexOf function to see if the string contains one of your illegal characters e.g.

    Code:
    var myStr = document.forms[0].txtName.value;
    if (myStr.indexOf('<') == -1)
    { 
    alert('Illegal Characters not allowed');
    }

    Comment

    • xNephilimx
      Recognized Expert New Member
      • Jun 2007
      • 213

      #3
      Originally posted by Uncle Dickie
      You can use the indexOf function to see if the string contains one of your illegal characters e.g.

      Code:
      var myStr = document.forms[0].txtName.value;
      if (myStr.indexOf('<') == -1)
      { 
      alert('Illegal Characters not allowed');
      }
      Even better, you can use regular expressions, that way you wont have to check for each individual character.

      something like:

      Code:
      if( myStr.match(/[\<\>!@#\$%^&\*,]+/i) ) {
          alert('Illegal characters found!');
      }
      You see that this way you only need one 'if' check, thanks to regex.
      Here's a link to a great site if you want to learn more about regex:
      Regular-Expressions.inf o - Regex Tutorial, Examples and Reference - Regexp Patterns

      Comment

      • Uncle Dickie
        New Member
        • Nov 2008
        • 67

        #4
        Thanks for the link - I had seen code like that and not understood it so glossed over it!

        Comment

        • xNephilimx
          Recognized Expert New Member
          • Jun 2007
          • 213

          #5
          You're welcome, Uncle Dickie. Since I learnt regexp, I can't live without it XD

          Comment

          • uicouic
            New Member
            • Jan 2009
            • 24

            #6
            Thanks alot guys. Really appreciate it. Hope to hear from you guys again.
            Cheers! :)

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              another good page to look at regarding this matter is here ... depending on your needs you should have a look at the 'shortcuts' to match complete character-classes like:

              [CODE=javascript]
              var s = 'foo>bar';

              if (/\W/g.test(s)) {
              alert('found a illegal char');
              }
              [/CODE]
              which uses the \W shortcut to classify the match for all chars that are not contained by the basic latin alphabet ... this often shortens the regExp and make it better readable ...

              kind regards

              Comment

              Working...