File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating email in javascript?

 
shivang sarawagi
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me how do i validate an email in javascript ??
 
olivier dutranoit
Ranch Hand
Posts: 81
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohan Mehra
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20187
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic