Saturday, 18 January 2014

Validate Email Address in JavaScript

Description:

  In this article, I am going to write JavaScript code examples to Validate Email in JavaScript by using different type of  Regular Expression filters in client side.

Summary:


Validate Email Address in JavaScript using Regular Expression - 1

   You can use and alter the following JavaScript function to Check and Validate an Email address, then you can say confirmation to user about the user has entered valid Email ID or not.
<html>
<head>
<script>

function checkEmail() {

    var email = document.getElementById('txtEmailID').value;
    var filter = /[\w-]+@([\w-]+\.)+[\w-]+/;

    if (!filter.test(email)) {
    alert('Please enter a valid email address');
    return false;
 }
  else{
  alert('Nice!..you have entered valid email address');
  }
}
</script>
</head>
<body>

<h1>Validate Email in JavaScript using Regular Expression</h1>

<input type='text' id='txtEmailID'/>
<button type="button" onclick="checkEmail()">Validate Mail</button>

</body>
</html> 


Validate Email Address in JavaScript using Regular Expression - 2

   You can use and alter the following JavaScript function to Validate an Email Address from client side.
<html>
<head>
<script>

function checkEmail() {

    var email = document.getElementById('txtEmailID').value;

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

    if (!filter.test(email)) {
    alert('Please enter a valid email address');
    return false;
 }
  else{
  alert('Nice!..you have entered valid email address');
  }
}
</script>
</head>
<body>

<h1>Validate Email in JavaScript using Regular Expression</h1>

<input type='text' id='txtEmailID'/>
<button type="button" onclick="checkEmail()">Validate Mail</button>

</body>
</html> 


Validate Corporate Email in JavaScript using Regular Expression - 3

   You can use the following JavaScript function for Domain specific Email validation in JavaScript from client side. Here, if you enter the email address with domains gmail.com,yahoo.com,hotmail.com and live.com, the checkMail function throws alert 'Please enter a valid email address'. By using this you can force user to enter only Corporate Email Address and stop user from entering some unwanted Email Domain Address.
<html>
<head>
<script>

function checkEmail() {

    var email = document.getElementById('txtEmailID').value;

    var filter = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!live.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/

    if (!filter.test(email)) {
    alert('Please enter a valid email address');
    return false;
 }
  else{
  alert('Nice!..you have entered valid email address');
  }
}
</script>
</head>
<body>

<h1>Validate Domain specific Email Address in JavaScript using Regular Expression</h1>

<input type='text' id='txtEmailID'/>
<button type="button" onclick="checkEmail()">Validate Mail</button>

</body>
</html> 

Email validation in JavaScript using Regular Expression - 4

   You can use and alter the following JavaScript function to validate and test an Email Address from client side.
<html>
<head>
<script>

function checkEmail() {

    var email = document.getElementById('txtEmailID').value;

    var filter = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

    if (!filter.test(email)) {
    alert('Please enter a valid email address');
    return false;
 }
  else{
  alert('Nice!..you have entered valid email address');
  }
}
</script>
</head>
<body>

<h1>Email validation in JavaScript using Regular Expression</h1>

<input type='text' id='txtEmailID'/>
<button type="button" onclick="checkEmail()">Validate Mail</button>

</body>
</html> 


Validate case sensitive Email in JavaScript using Regular Expression - 5

   You can use the following JavaScript function to validate Case Sensitive Email Address in JavaScript. This function check and allow only lower case letters in email address.
<html>
<head>
<script>

function checkEmail() {

    var email = document.getElementById('txtEmailID').value;

    var filter = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/

    if (!filter.test(email)) {
    alert('Please enter a valid email address');
    return false;
 }
  else{
  alert('Nice!..you have entered valid email address');
  }
}
</script>
</head>
<body>

<h1>Validate case sensitive Mail ID in JavaScript</h1>

<input type='text' id='txtEmailID'/>
<button type="button" onclick="checkEmail()">Validate Mail</button>

</body>
</html> 


Validate Email Address in JavaScript using Regular Expression - 6

   You can use the following JavaScript function to check Email Address is valid or not in JavaScript using Regular Expression from client side.
<html>
<head>
<script>

function checkEmail() {

    var email = document.getElementById('txtEmailID').value;

    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

    if (!filter.test(email)) {
    alert('Please enter a valid email address');
    return false;
 }
  else{
  alert('Nice!..you have entered valid email address');
  }
}
</script>
</head>
<body>

<h1>Validate Email in JavaScript using Regular Expressiont</h1>

<input type='text' id='txtEmailID'/>
<button type="button" onclick="checkEmail()">Validate Mail</button>

</body>
</html> 


Advertisements
Advertisements

No comments:

Post a Comment