Bookmark and Share

Email Regular Expression

Javascript email regular expression tutorial, validate email address using js regex.

Required regex to validate email address is:

/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

Email Regex Javascript Code

<script type="text/javascript">

function RegexEmail(emailInputBox){    
   var emailStr = document.getElementById(emailInputBox).value;  
   var emailRegexStr = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   var isvalid = emailRegexStr.test(emailStr); 
 
   if (!isvalid) {
 alert('Invalid email address!');
 emailInputBox.focus;
 }
 }

</script>

Place the javascript code above between to your HEAD tags, or you can also include into a js file.
To use javascript externally put the code below between HEAD tags.

<script src="myRegex.js" type="text/javascript"></script>

How to use JS code in HTML BODY

Now our email address regular expression javascript code is ready to use, we have to put input box and a button control in our html. Copy paste the code below in your html after BODY tag where ever you want.

<input name="emailInput" id="emailInput" maxlength="60" />
<input id="btn1" type="button" value="Validate Email" onclick="return RegexEmail('emailInput')" />

Testing Our Email Regex JS

Email Address: