regular expression to check a string is alphanumeric only

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    regular expression to check a string is alphanumeric only

    I want to check if the user enters alphabet or numbers only in the
    text box. If the user enters non-alphabet or non-numbers, I should pop
    up a message and doesn't allow the user to do that. I am using regular
    expression to do the checking. But it seems it always return false.
    What did I miss? please advise. thanks!!

    <script type="text/javascript">
    function checkkey(obj)
    { var re = /^[a-zA-Z_0-9]$/;
    alert(re.test(o bj.value));
    if (! re.test(obj.val ue))
    { alert("Please enter alphanumeric only");
    }
    }
    </script>

    <form name="InputForm ">
    <P><input type="text" name="username"
    onKeyPress="che ckkey(InputForm .username)">
    </form>
  • Evertjan.

    #2
    Re: regular expression to check a string is alphanumeric only

    Matt wrote on 21 sep 2004 in comp.lang.javas cript:
    [color=blue]
    > I want to check if the user enters alphabet or numbers only in the
    > text box. If the user enters non-alphabet or non-numbers, I should pop
    > up a message and doesn't allow the user to do that. I am using regular
    > expression to do the checking. But it seems it always return false.
    > What did I miss? please advise. thanks!!
    >
    > <script type="text/javascript">
    > function checkkey(obj)
    > { var re = /^[a-zA-Z_0-9]$/;
    > alert(re.test(o bj.value));
    > if (! re.test(obj.val ue))
    > { alert("Please enter alphanumeric only");
    > }
    >}
    > </script>
    >
    > <form name="InputForm ">
    > <P><input type="text" name="username"
    > onKeyPress="che ckkey(InputForm .username)">
    > </form>[/color]

    try:

    <script type="text/javascript">
    function checkkey(v) {
    if (/\W/.test(v.value)) {
    alert("Please enter alphanumerics only");
    return false;
    }
    return true;
    }
    </script>

    <form>
    <input type="text" onKeyUp="return checkkey(this)" >
    </form>

    =============

    \W is a shortcut for [^a-zA-Z0-9_]

    onKeyPress() will be one letter late, so use onKeyUp()

    "this" will uniquely reference your object

    the "return true/false" will prohibit the disallowed char displaying



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)

    Comment

    • RobG

      #3
      Re: regular expression to check a string is alphanumeric only

      Evertjan. wrote:
      [color=blue]
      >
      > the "return true/false" will prohibit the disallowed char displaying
      >[/color]

      Doesn't seem to for me in any browser I tried.

      If the requirement is to not display disallowed characters
      at all, the following works:

      <script type="text/javascript">
      var a = '';
      function checkKey(v) {
      if (/\W/.test(v.value)) {
      alert('Please enter alpha numeric characters only');
      v.value = a;
      }else{
      a = v.value;
      }
      }
      </script>

      Comment

      • Evertjan.

        #4
        Re: regular expression to check a string is alphanumeric only

        RobG wrote on 22 sep 2004 in comp.lang.javas cript:
        [color=blue]
        > Evertjan. wrote:
        >[color=green]
        >>
        >> the "return true/false" will prohibit the disallowed char displaying
        >>[/color]
        >
        > Doesn't seem to for me in any browser I tried.
        >[/color]

        Did you try IE? That's the one tested OK by me.

        Please do not implement my code but test it first.

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress,
        but let us keep the discussions in the newsgroup)

        Comment

        • RobG

          #5
          Re: regular expression to check a string is alphanumeric only

          Evertjan. wrote:[color=blue]
          > RobG wrote on 22 sep 2004 in comp.lang.javas cript:
          >[color=green]
          >>Evertjan. wrote:
          >>[color=darkred]
          >>>the "return true/false" will prohibit the disallowed char displaying[/color]
          >>
          >>Doesn't seem to for me in any browser I tried.[/color]
          >
          > Did you try IE? That's the one tested OK by me.[/color]

          Yes, IE 6.0.2800.1106.x psp2. Also Firefox 0.9.3 and Mozilla
          1.7.2. The code I posted works in all these browsers.

          Comment

          • RobG

            #6
            Re: regular expression to check a string is alphanumeric only

            Evertjan. wrote:
            [color=blue]
            > Did you try IE? That's the one tested OK by me.
            >[/color]

            Yes, IE 6.0.2800.1106.x psp2. Also Firefox 0.9.3 and Mozilla
            1.7.2

            Comment

            • Evertjan.

              #7
              Re: regular expression to check a string is alphanumeric only

              RobG wrote on 23 sep 2004 in comp.lang.javas cript:
              [color=blue]
              > Evertjan. wrote:[color=green]
              >> RobG wrote on 22 sep 2004 in comp.lang.javas cript:
              >>[color=darkred]
              >>>Evertjan. wrote:
              >>>
              >>>>the "return true/false" will prohibit the disallowed char displaying
              >>>
              >>>Doesn't seem to for me in any browser I tried.[/color]
              >>
              >> Did you try IE? That's the one tested OK by me.[/color]
              >
              > Yes, IE 6.0.2800.1106.x psp2. Also Firefox 0.9.3 and Mozilla
              > 1.7.2. The code I posted works in all these browsers.[/color]

              I thought you were referring to the code I(!) posted not working?

              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress,
              but let us keep the discussions in the newsgroup)

              Comment

              Working...