Validate e-mail with a regular expression

Simple example of validating a string to check if it is an e-mail.
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<script>

  var myEmail = "bart_b@gmail.com";
  var anotherEmail = "bartb@gmail.com.pl";
  var lastEmail = "bartbgmail.com";
    
  function checkEmail(email) {
      var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
      return r.test(email);
  }
   
   console.log("Is " + myEmail + " a proper e-mail? --> " + checkEmail(myEmail));
   console.log("Is " + anotherEmail + " a proper e-mail? --> " + checkEmail(anotherEmail));
   console.log("Is " + lastEmail + " a proper e-mail? --> " + checkEmail(lastEmail));

</script>

</body>
</html>

Responses

0 Replies