wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Validating email in javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Validating email in javascript?" Watch "Validating email in javascript?" New topic
Author

Validating email in javascript?

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 148
Can anyone tell me how do i validate an email in javascript ??
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Hi,

If you want tu use client-side validation, with javascript, the best way is to use regular expressions.
Plenty of examples on the web.

if you want use HTML5, you can use the build in validators (no script needed) :



BUT, html5 is not supported very well at hte moment :-)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

You can use a regular expression. Search the web for examples.

Also, be sure to validated on the server as well. You can never trust what the client does.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mohan Mehra
Ranch Hand

Joined: Jul 28, 2011
Posts: 72
function echeck(field) {
var str = field.value;
var at = "@";
var dot=".";
var nos="0123456789";
var lat=str.indexOf(at);
var lstr=str.length;
var ldot=str.indexOf(dot);
var com=str.substring(str.lastIndexOf(dot)+1);

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;
}


for(var i=0;i<com.length;i++){
if ((com.charAt(i)<='0' && com.charAt(i)<='9')){
alert("Invalid e-Mail ID anuj >>"+com);
return false;
}
}


if (str.indexOf(dot,(lat+2))==-1){

return false;
}

if (str.indexOf(" ")!=-1){

return false;
}


var elen=str.substring(str.indexOf(dot)+1);

if (elen.length<2){
return false;
}
return true;
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

rajshekar mehra, please use code tags when posting code to the forums.

And why use all that code when one line with a regular expression can do the job?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18711
    
    8

If you're going to "validate" an e-mail address, make sure you do it right. I've seen at least one blog post where the writer stopped doing business with a company because their website rejected his e-mail address (which had a dash in it, if I remember right).

In the web application I work on, we don't validate e-mail addresses. It's up to the user to put in their e-mail address correctly. You might want to reconsider whether you even need to validate e-mail addresses. What benefits does that validation provide?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Validating email in javascript?