• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validating email in javascript?

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me how do i validate an email in javascript ??
 
Ranch Hand
Posts: 81
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic