SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression – Function Expected

    Hello

    I am having an issue getting this regular expression to work in IE 7. This code works perfect in FireFox & Netscape, but it does not work in IE7. The error IE 7 gives me is:

    Function Expected

    Below is the code. Does anyone know why I am getting this error?

    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    
    function test()
    {
       //-- Regular Expression to validate the entered Zip Code
       regZip = new RegExp(/(^\d{5}$)/);
          
       if( regZip( document.getElementById("zipCode").value ) )
       {
          alert("Thank you for entering a valid Zip Code");
       }
       else
       {
          alert("Entered Zip Code is not valid.");
       }
    }
    
    </script>
    </head>
    <body>
    
    <input type="Text" id="zipCode" />
    &nbsp;
    <input type="Button" value="validate" onClick="test();" />
    
    </body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    regZip= /(^\d{5}(\-\d{4})?$)/;
    if( regZip.test(document.getElementById("zipCode").value ) ) {
    alert("Thank you for entering a valid Zip Code");
    }
    else alert("Entered Zip Code is not valid.");

    Not a great idea to call the function 'test'....

  3. #3
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for pointing out that I needed to call the function…

    regZip.test( )

    As for the name, this is just a proof of concept but I would agree with you, test() would not be a good name for production code.

    Thank you again for the help.


Bookmarks

Posting Permissions

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