GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Web  >  java Script
Next Question 
 java Script  |  Question 1 of 24    Print  
How to use email validation in Java script.Like User can't enter invalid email address?

  
Total Answers and Comments: 6 Last Update: September 07, 2009     Asked by: mrbaliram 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: vinod_arya
 

u have basically 2 search occurance of @ symbol and at least 2 "."(period) signs in the mail id which u can search using String functions



Above answer was rated as good by the following members:
malkaps
February 02, 2006 07:32:35   #1  
bicu Member Since: September 2005   Contribution: 21    

RE: how i will use email validation in java script,

First of all here is the javascript function that tests if an email address is valid.
It checks for a lot of things: to see if the email contains a @ sign and a dot checks to see if the @ is before the dot and so on.

<script>
function isMail(str)
{
var at @ ;
var dot . ;
var lat str.indexOf(at);
var lstr str.length;
var ldot str.indexOf(dot);
if (str.indexOf(at) -1) return false;
if (str.indexOf(at) -1 || str.indexOf(at) 0 || str.indexOf(at) lstr) return false;
if (str.indexOf(dot) -1 || str.indexOf(dot) 0 || str.indexOf(dot) lstr) return false;
if (str.indexOf(at (lat+1))! -1) return false; if (str.substring(lat-1 lat) dot || str.substring(lat+1 lat+2) dot) return false;
if (str.indexOf(dot (lat+2)) -1) return false;
if (str.indexOf( )! -1) return false;
return true;
}

// the next function will be used to validate the form. I made this as a separate function because you might want to validate more fields.
function validate_form()
{
if (!isMail(document.form1.mail.value))
{
alert( Please enter a valid email address );
document.form1.mail.focus();
return false;
}
// if the value entered is not a valid email address
// we show a message focus on the field and return false.
// returning false will make sure the form is not submitted

// if we got here it means the email address is valid
return true;
// return true means that the form is submitted.
}

</script>

After placing this function in the code here is what you need to put in the form.

Let's say the form only has the email address and a submit button.

<form action / method post onsubmit return validate_form() name form1 >
<input type text name email value >
<input type submit name submit_button value continue >
</form>


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 26, 2006 09:27:01   #2  
vinod_arya Member Since: September 2005   Contribution: 6    

RE: how i will use email validation in java script,

u have basically 2 search occurance of @ symbol and at least 2 . (period) signs in the mail id which u can search using String functions


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 06, 2006 23:27:01   #3  
michael        

RE: how i will use email validation in java script,
a better way is to use regular expression
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
December 06, 2006 10:25:56   #4  
toneerajkumar Member Since: October 2005   Contribution: 1    

RE: How to use email validation in Java script.Like Us...
script for email validations// check if email field is blankif (theForm.Email.value ){alert( Please enter a value for the Email field. );theForm.Email.focus();return (false);}// test if valid email address must have @ and .var checkEmail @. ;var checkStr theForm.Email.value;var EmailValid false;var EmailAt false;var EmailPeriod false;for (i 0; i < checkStr.length; i++){ch checkStr.charAt(i);for (j 0; j < checkEmail.length; j++){if (ch checkEmail.charAt(j) && ch @ )EmailAt true;if (ch checkEmail.charAt(j) && ch . )EmailPeriod true; if (EmailAt && EmailPeriod) break; if (j checkEmail.length) break; } // if both the @ and . were in the stringif (EmailAt && EmailPeriod){ EmailValid true break; }}if (!EmailValid){alert( The email field must contain an @ and a . . );theForm.Email.focus();return (false);}
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 03, 2007 07:24:40   #5  
Saurabh        

RE: How to use email validation in Java script.Like Us...
/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2 4})+$/

Use this regular expression

You can be more strict by using .com .biz .org .arpa etc in regular expression

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 07, 2009 03:39:07   #6  
SwapnilSwapnil Member Since: September 2009   Contribution: 2    

RE: How to use email validation in Java script.Like User can't enter invalid email address?

There are many varieties of email validation. This one is simple but effective.

function isValidEmail(str) {

return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape