www.webdeveloper.com
Results 1 to 7 of 7

Thread: Need help with RegExp

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    12

    Post Need help with RegExp

    I need to validate that the the user has entered a ten digit number into the text box, and need some help with the code.
    Here is what I have:

    <html>
    <head>
    <title>Checking with RegExp</title>
    </head>
    <body>
    <script type="text/javascript">
    function checknumber() {
    var tendigitnum = document.getElementById("text").value;
    var check=/^\d{10}$/;
    if (check.test(tendigitnum)) {
    window.alert("You entered"+tendigitnum);
    }
    else {
    window.alert("Please enter a valid 10 digit number");
    }
    </script>
    <form method="post" name="submitnumber" onsubmit="checknumber()">
    <input type="text" id="text" name="inputnumber" size="15"></input>
    <input name="check" value="Check" type="submit"></input>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    The regex is formatted properly, it looks like; you want to make sure that the whole string is nothing but 10 digits.

    You are trying to compare the value of an element that does not exist. You don't have an element named "text", you have a text input named "inputnumber". So:
    Code:
    function checknumber() {
    var tendigitnum = document.getElementById("inputnumber").value;
    var check=/^\d{10}$/;
    if (check.test(tendigitnum)) {
    window.alert("You entered"+tendigitnum);
    }
    else {
    window.alert("Please enter a valid 10 digit number");
    }
    Always be on the look out for JS error messages.. they can tell you much about what the issue is.

    Also, you can remove "window." from before alert; you don't need it.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  3. #3
    Join Date
    Mar 2011
    Posts
    12

    It still won't work

    The page should display an alert box telling whether the number was ok or not at the end. It still doesn't
    here is the code:

    <html>
    <head>
    <title>Checking with RegExp</title>
    </head>
    <body>
    <script type="text/javascript">
    function checknumber() {
    var tendigitnum = document.getElementById("inputnumber").value;
    var check=/^\d{10}$/;
    if (check.test(tendigitnum)) {
    window.alert("You entered"+tendigitnum);
    }
    else {
    window.alert("Please enter a valid 10 digit number");
    }
    </script>
    <form method="post" name="submitnumber" onsubmit="checknumber()">
    <input type="text" id="text" name="inputnumber" size="15"></input>
    <input name="check" value="Check" type="submit"></input>
    </body>
    </html>

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Everything looks correct, except for the "window.alert" parts. Change those to just "alert". If that doesn't fix it, let me know. I'll take a closer look, in the meantime.

    I just noticed - you are not doing anything to prevent the form from being submit. Set your onsubmit to "return checknumber();", then:
    Code:
    alert("You entered"+tendigitnum);
    return true;
    .. and ..
    Code:
    alert("Please enter a valid 10 digit number");
    return false;
    Last edited by WolfShade; 03-18-2011 at 03:43 PM.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  5. #5
    Join Date
    Mar 2011
    Posts
    12

    Still will not work

    here is my new code, and it is still not working. Please help
    thanks

    <html>
    <head>
    <title>Checking with RegExp</title>
    </head>
    <body>
    <script type="text/javascript">
    function checknumber() {
    var tendigitnum = document.getElementById("inputnumber").value;
    var check=/^\d{10}$/;
    if (check.test(tendigitnum)) {
    window.alert("You entered"+tendigitnum);
    }
    else {
    window.alert("Please enter a valid 10 digit number");
    }
    </script>
    <form method="post" name="submitnumber" onsubmit="checknumber()">
    <input type="text" id="text" name="inputnumber" size="15"></input>
    <input name="check" value="Check" type="submit"></input>
    </body>
    </html>

  6. #6
    Join Date
    Dec 2010
    Posts
    207
    Quote Originally Posted by codybonvo View Post
    here is my new code, and it is still not working. Please help
    Please help yourself and stop ignoring the error console.


    Q: I found this code...
    A: Then find its author.

  7. #7
    Join Date
    Mar 2011
    Posts
    12

    thanks for the help

    Ok, I got it to work with some more work.
    Thank you for the help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles