aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes Validating email in javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply 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: 116
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: 56162
    
  13

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.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Mohan Mehra
Ranch Hand

Joined: Jul 28, 2011
Posts: 63
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: 56162
    
  13

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: 16482
    
    2

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?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Validating email in javascript?
 
Similar Threads
E-mail address validation
WA #1.....word association
JAvascript to sendemail
java script for sending mails from webpage
HTML, FORMs and AJAX inside Email clients?