Prashant More

Prashant More

  • NA
  • 87
  • 166.2k

Need A code for Email Validation on keypress event

May 27 2008 9:13 AM

Hello All,

I need a code for email validation on keypress event.If improper email id is entered a messagebox must have to appear showing a perticular message.I tried the as shown in the following but it could not working.Please anybody send me the code its hurry for me.

if((document.getElementById('ctl00_MC_PlaceHolder_txt_email').value).replace(/^\s*|\s*$/,"") != "")
//if (document.getElementById('<%=txt_mailid.ClientID %>').value!= "")
{
var mail1=document.getElementById('ctl00_MC_PlaceHolder_txt_email').value;
var i1 = mail1.indexOf('@');
var i2 = mail1.lastIndexOf('@');
var i3 = mail1.indexOf('.');
if (mail1.length<=3)
{
alert("Your E-mail address is too small");
document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus();
return false;
}

if(i1==-1)
{
alert(" E-mail address must have a '@' ");
document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus();
return false;
}
if(i3==-1)
{
alert(" E-mail address must have a '.' ");
document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus();
return false;
}

if (i3+1==mail1.length)
{
alert(" E-mail address cannot contain '.' in end");
document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus();
return false;
}

if(i1!=i2)
{
alert(" E-mail address cannot contain two '@' signs");
document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus();
return false;
}
}
if (document.getElementById('ctl00_MC_PlaceHolder_txt_email').value != "")
{
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-_.@";
var checkStr = document.getElementById('ctl00_MC_PlaceHolder_txt_email').value;
var allValid = true;
for (i = 0;i<checkStr.length;i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))

break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter, digit and \"@\" characters in the \"E-mail\" field.");
document.getElementById('ctl00_MC_PlaceHolder_txt_email').focus();
return false;
}
}

My Email Ids are

[email protected]

[email protected]

Thanks & Regards

Prashant S. More


Answers (8)