Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
JavaScript Email Address validation using Regular Expression

JavaScript Email Address validation using Regular Expression

01 Feb 2024
Intermediate
147K Views
2 min read
Learn with an interactive course and practical hands-on labs

Free Javascript Course: Learn Javascript In 21 Days

We can validate email address at client side and server side. To validate email address on client side, we can use java script with regular expression. Java script can check the regular expression pattern for valid email address. We have different regular expression patterns for validating email address. In this article, I am sharing some useful cross browsers regular expression patterns for validating email address with java script and jquery.

Simple regular expression to validate email address

Below is simple regular expression to validate an email address. It will accept email address in upper case also.

 <script type="text/javascript">
function validateEmail(email)
{
 var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
 if (reg.test(email)){
 return true; }
 else{
 return false;
 }
} 
</script> 

Read More: Javascript Developer Salary in India

Regular expression to validate case-sensitive email address

Below is regular expression to validate an email address with lower case chars.

 <script type="text/javascript">
function validateCaseSensitiveEmail(email) 
{ 
 var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
 if (reg.test(email)){
 return true; 
}
 else{
 return false;
 } 
} 
</script> 

Regular expression to validate free/domain specific email address

Below is regular expression to validate domain specific email address.

 <script type="text/javascript">
function validateFreeEmail(email) 
{
 var reg = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/
 if (reg.test(email)){
 return true;
 }
 else{
 return false;
 }
} 
</script> 

JQuery - Email address validation with example

 <html>
<head>
<script type="text/javascript">
$(document).ready(function(e){
 $('#btnSubmit').click(function(){ 
 var email = $('#txtEmail').val();
 if ($.trim(email).length == 0) {
 alert('Please Enter Valid Email Address');
 return false;
 }
 if (validateEmail(email)) {
 alert('Valid Email Address');
 return false;
 }
 else {
 alert('Invalid Email Address');
 return false;
 }});
});
</script>
</head> <body>
Email Address: <input type="text" id="txtEmail"/><br/>
<input type="submit" id="btnSubmit" Value="Submit" />
</body>
</html> 

Read More: 50+ Javascript Interview Questions and Answers

What do you think?

In this article I try to explain, how you can validate different-different email address acc. to your need. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Take our Javascript skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this