ryanjohnsond
ryanjohnsond

Reputation: 503

RegEx passes but fails on browsers

This RegEx for an email passes on http://scriptular.com, but fails all the browsers. Any ideas why? i tried a number of ways; but at this point, I am clueless.

   <!doctype html>
   <html lang="en">
   <head>
   <meta charset="UTF-8">
   <title>RegEx Test</title>

    <script type="text/javascript">
      function saveSignUp(){

      var warning = "The following field(s) require an entry: \n \n";
      var same = warning;
      var email = document.forms[0].email.value;
      // FIRST TEST
      /*var regExEmail= /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+/;*/
      // SECOND TEST
      var regExEmail= /[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-zA-Z]+/;
      if(email == ""){ warning += " - Email is required \n"; }
      else if(email != regExEmail){ warning += " - E-mail not formatted properly \n"; }

      if (warning == same){
        return true; 
      } 
      else { 
        alert(warning);     
      }
    return false;
    }
   </script>
    </head>
    <body>
      <form method="post" onsubmit="return saveSignUp()" action="signupSucces.html" >
        <input type="text" placeholder="Email" name="email">
        <input type="submit" class="button wide" value="Request an Account">        
      </form>
    </body>
   </html>

Upvotes: 0

Views: 43

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

if(email != regExEmail)

This is not how you test a regex match. Use this instead:

if (!regExEmail.test(email))

And, you should encose your regex within ^..$ to check the whole string matches, not just a substring of it.

var regExEmail= /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-zA-Z]+$/;

Upvotes: 2

Related Questions