Please enable Javascript to correctly display the contents on Dot Net Tricks!

JavaScript Email Address validation using Regular Expression

  Author : Shailendra Chauhan
Posted On : 04 Jul 2012
Total Views : 72,126   
Updated On : 25 Jul 2013
 

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> 

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> 
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.



Free Interview Books
 
COMMENTS (0)
11 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

Know More
6 MAR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
6 MAR
Angular 2 Master Class Fast Track Online Development Training (Online)

10:30 AM- 01:30 PM IST

Know More
27 FEB
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

Know More
25 FEB
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM-4:30 PM IST

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
14 FEB
.NET Development (Classroom)

11:00 AM-12:00 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

15 JAN
PPC Marketing (Classroom)

04:00 PM-05:30 PM

10 JAN
ASP.NET MVC with AngularJS Development (Online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

5 NOV
ASP.NET MVC with AngularJS Development (Classroom)

08:00 AM-09:30 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+