Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Regex Test</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <form>
    <input type="text" id="email" value="didxga@gmail.comblah@foo.com" size="40"/><br/>
    Use RegExp Object to test<br/>
    <input type="button" value="validate using RegExp notation" onclick="validateEmail('RegExp')"/><br/>
    Use slash notation to test<br/>
    <input type="button" value="validate using slash notation" onclick="validateEmail('slash')"/><br/>
  </form>
</body>
</html>
 
function validateEmail(type) {
  
  var email = document.getElementById("email").value;
  var regex;
  
  if(type==="RegExp") {
  
    var regex = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
    
  } else if(type==="slash") {
  
   var regex = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
                                              
  }
  
                                         
  if(regex.test(email))  {
     
       console.log("email matched");
       alert("email matched");
     
  } else {
    
       console.log("email mismatched");
       alert("email mismatched");
    
  }
                                         
                                    
}                               
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers