TextBox validation using regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathewgk80
    New Member
    • Sep 2007
    • 103

    TextBox validation using regex

    Hi,

    I am trying to check whether the textbox contains single quote,double quote and < and > symbols.

    I got the regex to check all the requirements.
    its as shown below.

    str=(document.g etElementById(' txtEnter')).val ue;

    str.match(/([\<\])(1,))* ([\>])/)==null (for checking < and > symbol)

    str.match(["'"])==null (for checking single quote)

    and

    str.match(['"'])==null) (for checking double quotes.)

    Now i give all these conditions as

    if(str.match(/([\<\])(1,))* ([\>])/)==null && str.match(["'"])==null && (str.match(['"'])==null))

    alert("error");

    else
    alert("no error");

    It works fine.

    In the above code i used str.match() three times. I need to use str.match() only one time and need to check whether the textbox contains < or > or single quote or double qoute.

    Please help me..

    Thanks in Advance.

    Mathew.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    here is a short example ... you may just use the | for the OR :)

    [CODE=javascript]var s = 'fo\'o';

    alert(/[<]|[>]|[']|["]/g.test(s)); [/CODE]
    kind regards

    PS: and please use the code-tags when posting source code.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      if you are trying to remove html, you'd better be a lot more thoughtful than the regexps i see posted unless you also validate on the server.

      there's more than one way to say <script, onclick="alert( 'bad')", etc...

      check out the xss cheatsheet for more info.

      Comment

      Working...