match entire string (regular expression & javascript)

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

    #1

    match entire string (regular expression & javascript)

    Hello, I'm trying to perform a very simple validation of user input. I
    want to verify that the user entered a six-digit string consisting
    entirely of numbers. So anything from 000000 to 999999 is considered
    valid. The problem that I'm having is getting the validation to work on
    the entire string. In other words, 000000 is okay but 000000000000 is
    also returning as a match. Here's a quick code block...I have something
    along these lines....

    /*************** *************** *************** *******/
    <html>
    <body>
    <INPUT name="txtNumber Field" type="text" id="txtNumberFi eld">
    <INPUT onClick="fnTest NumberField()" id=Button1 type=button value="Test
    Number!" name=btnTest>
    <script language=javasc ript>
    function fnTestNumberFie ld()
    {
    var sNumberValue = document.getEle mentById("txtNu mberField").val ue;

    if (sNumberValue.m atch(/\A[0-9]{6}\z/))
    {
    alert("match");
    } else {
    alert("no match");
    }
    }
    </script>
    </body>
    </html>
    /*************** *************** *************** *******/

    That is failing when I enter 123456 into the textbox. Why, though? I
    know I can replace...

    if (sNumberValue.m atch(/\A[0-9]{6}\z/))

    ....with something like...

    if (sNumberValue.l ength == 6 && sNumberValue.ma tch(/[0-9]{6}/))

    ....or I could assign a maxlength to the input box, of course. The thing
    is, I really want to know WHY the regular expression isn't responding
    as I'd expect. Is there a syntax error somewhere in the code?

    Any suggestions would be appreciated. Thanks!

  • Random

    #2
    Re: match entire string (regular expression &amp; javascript)


    GreggTB wrote:[color=blue]
    > Hello, I'm trying to perform a very simple validation of user input. I
    > want to verify that the user entered a six-digit string consisting
    > entirely of numbers. So anything from 000000 to 999999 is considered
    > valid. The problem that I'm having is getting the validation to work on
    > the entire string. In other words, 000000 is okay but 000000000000 is
    > also returning as a match. Here's a quick code block...I have something
    > along these lines....
    >
    > /*************** *************** *************** *******/[/color]

    ....
    [color=blue]
    > if (sNumberValue.m atch(/\A[0-9]{6}\z/))
    > {
    > alert("match");
    > } else {
    > alert("no match");
    > }
    > }[/color]


    Use ^ to match the beginning of the string and $ to match the end.
    Also, \d is the same as [0-9].
    /^[0-9]{6}$/ or /^\d{6}$/

    Both work.

    Comment

    • GreggTB

      #3
      Re: match entire string (regular expression &amp; javascript)

      Excellent! I knew there had to be a way to do that but I'm (obviously)
      really new to regular expressions. Thank you so much!

      Comment

      Working...