email validation in textbox

Posted by Klbaiju under ASP.NET on 3/29/2012 | Points: 10 | Views : 9992 | Status : [Member] | Replies : 9
Hi,
in my application there is a textbox for entering email id .if the email format is not correct i want to print an alert box on leaving the text field.
ie if press tab alert should display
how it is possible.

Regards

Baiju




Responses

Posted by: Savariya on: 3/29/2012 [Member] Starter | Points: 25

Up
0
Down
use this regular expression

\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 5/23/2012 [Member] Starter | Points: 25

Up
0
Down
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">

Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>



function validateForm()

{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}


HOPE THIS WILL HELP

REGARDS

sriram

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kavi.Sirius on: 5/24/2012 [Member] Starter | Points: 25

Up
0
Down
hi,

You just use the Regularexpressionvalidator to check the email format or not

refer this link for example

http://asp-net-example.blogspot.in/2009/02/aspnet-regularexpressionvalidator.html


Regards
Kavi.n

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavan_Kumar on: 5/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
You can use the below regex to match the email pattern.

string MatchPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

if ((txt1.text == null) || !(Regex.IsMatch(txt1.text, MatchPattern )))

{

MessageWindow msgwindow= new MessageWindow("Invalid eMail ID.")
msgwindow.Show();

}


I hope it helps.

With best rgds,
Pavan



Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 6/13/2012 [Member] Bronze | Points: 25
Posted by: Muralidosscm on: 7/4/2012 [Member] Starter | Points: 25

Up
0
Down
<asp:TextBox ID="txtEmail" runat="server" CssClass="text"> </asp:TextBox>

<asp:RegularExpressionValidator ID="regValidator" runat="server" ErrorMessage="Email is not vaild." ForeColor="Red" ControlToValidate="txtEmail" ValidationExpression="^\s*(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*\s*$" ValidationGroup="register">Email is not vaild.</asp:RegularExpressionValidator>

Regards
Muralidoss M

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay13mar on: 11/25/2012 [Member] Starter | Points: 25

Login to post response