Validating E-mail ID using Javascript

This junk of code will validate the text control whether given e-mail is valid or not



< input id="Text1" type="text" />
< input id="Button2" type="button" value="button" onclick="echeck();"/>


when the user clicks on button onclick event will call echeck function and email id is validated



< script type="text/javascript" >

function echeck()
{
var str=document.getElementById("Text1").value;
var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1)
{
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
{
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==(lstr-1))
{
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(at,(lat+1))!=-1)
{
alert("Invalid E-mail ID")
return false
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
{
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(dot,(lat+2))==-1)
{
alert("Invalid E-mail ID")
return false
}
if (str.indexOf(" ")!=-1)
{
alert("Invalid E-mail ID")
return false
}
}
</script >


If given e-mail is not proper e-mail id then a pop up alert will appear else e-mail id is in correct in form


Attachments

  • emailvalidation (29220-15311-email.html)
  • Comments

    Author: Mrs. Meetu Choudhary Nanda16 Jun 2009 Member Level: Gold   Points : 1

    Instead of removing the angular braces use & lt for < and & gt for > without space...

    ++
    Thanks and Regards
    Meetu Choudhary
    Editor

    Author: Maya16 Jun 2009 Member Level: Bronze   Points : 0

    thanks its so useful to me

    Thanks and Regards
    Maya Madhav

    Author: saranya11 Jan 2010 Member Level: Silver   Points : 0

    its working fine. really a helpful one

    Author: amol00712 Jan 2010 Member Level: Silver   Points : 0

    nice code thanks.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: