Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    error getting using in regular expression!!!

    <head>
    <script>
    function m1()
    {
    var regex=\(^/d{6}$)\; // error getting here i dont knwo why
    if(document.write.myform.pin.value.search(regex)==-1)
    {
    alert("invalid pin");
    return false;
    }
    }
    </script>

    </head>

    <body>
    <center>
    <form action="http://www.google.com" method="get" name="myform" onsubmit="return m1()">
    enter pin<input type="text" name="pin" value="pin" />

    </center>
    </body>

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    You are getting the error because you are using forward slash where it should have been a backslash, and vice versa.
    The correct way is:
    Code:
    regex = /(^\d{6}$)/;

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Try this:-

    Code:
    <!DOCTYPE html>
    <head>
    </head>
    
    <body>
    
    <form id="myform" action="http://www.google.com" method="get" onsubmit = "return m1()">
    Enter pin<input type="text" name="pin" id = "pin" value="pin" />
    <input type = "submit" value = "Submit" >
    </form>
    
    <script type = "text/javascript">
    
    function m1()  {
    var val = document.getElementById("pin").value;
    if (!/^\d{6}$/.test(val))  {
    alert ("Invalid pin - retype it");
    document.getElementById("pin").value = "";
    document.getElementById("pin").focus();
    return false;
    }
    }
    
    </script>
    
    
    </body> 
    </html>

    Be aware that the <center> tag is deprecated and obsolete. Use css for styling.
    Assigning a name to a form is also deprecated and obsolete. Use an id instead.
    Modern practice is to place scripts right in front of the </body> tag.
    document.write() is long obsolete and should never be used.


    "Political langauge - and with variations this is true of all political parties from Conservatives to Anarchists - is designed to make lies sound truthful and murder respectable, and to give the appearance of solidity to pure wind". - George Orwell, English novelist and journalist, 1903-1950.
    Last edited by Philip M; 08-03-2014 at 06:02 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    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
    •