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
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
Thanks & Regards
Prashant S. More
Niradhip ChakrabortyPosted Jun 3, 2008, 2:12 AM
I dont think there is any error in the code. Do you know how to debug java script code??? Please debug and check the flow
of the execution. Yes I am calling btnvalidate() function in page load but I am also calling isEmail() function
inside function btnvalidate().
if (!isEmail(document.getElementById("txtCmail").value)){
}
Inside function btnvalidate() I have written the above condition,which call the isEmail function.
For displaying message you can use alert(""); or confirm("");
Prashant MorePosted May 30, 2008, 3:07 AM
Hello Sir,
Thanks for your reply and i am extremly sorry sir because the code is not working.what happens when i put my name Prashant into the textbox it doesnot display any message and even when i put nothing and click on the button it does not display any message.Also i have doubt about the function
function isEmail(s){
if (s.search(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) == -1){
return false;
}
else{
return true;
}
}
The doubt is how this function called on button click?because in page_load event we just called btnvalidate().So sir please explain me all this and if any error in code then send me proper code.
Thanks & Regards
Prashant S. More
Niradhip ChakrabortyPosted May 29, 2008, 11:04 AM
Use javascript code in your.aspx page.
//Call this function in following way(btnSubmit is the Id of button
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnSubmit.Attributes.Add("onclick", "return btnvalidate()");
}
}
//Create asp button in your html code like this:
Just call the btnvalidate function on button click event.It will work.
Prashant MorePosted May 29, 2008, 6:29 AM
Hello Sir,
Thank you for your reply.but it could not work.I enter into textbox values 123dsf but it could not show me any messagebox().Sir if not possible on keypress event then please send me the code on buttonclick event.and how to call the function on button click event.?
Thanks & Regards
Prashant S. More
Niradhip ChakrabortyPosted May 29, 2008, 1:38 AM
function
isEmail(s){ if (s.search(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) == -1){ return false;}
else{ return true;}
}
Prashant MorePosted May 28, 2008, 5:15 AM
Hello Sir,
Thanks for your reply.But my problem could not solved.Actually i am working with a web application and there is not facility like MassageBox.Show().So sir please send me the Javascript code for email validation on keypress event.
Thanks & Regards
Prashant S. More
Niradhip ChakrabortyPosted May 28, 2008, 2:03 AM
As Scott said you should use regular expression validator. Incase u want to do it through coding.Here comes the code.
private void txtEmail_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (txtEmail.Text.Length > 0)
{
if (!rEMail.IsMatch(txtEmail.Text))
{
MessageBox.Show("Please provide valid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtEmail.SelectAll();
}
}
}
Scott LyslePosted May 27, 2008, 1:42 PM