.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

How to validate textBox for E-mail Id in C#.net

Posted By: susant     Posted Date: November 20, 2008    Points:2   Category :C#
Hi All
How to validate e-mail address in C#.Net aplication for one textBox..

susant
ThanX

Responses
Author: Akhil Raj       [Moderator]      
Posted Date: November 20, 2008     Points: 5   

hi mate,
You can do this by using regularexpressionvalidator control.
set the controltovalidate property to your textbox id and put the expression in ValidationExpression property.

For regular expressions check the following link:

http://regexlib.com/Search.aspx?k=email


Author: susant             
Posted Date: November 20, 2008     Points: 5   

I am using C#.Net with windows application so there is no property control to validate...

susant

Author: Amit Mehra             
Posted Date: November 21, 2008     Points: 5   

Try like this


private void textBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (textBox1.Text.Length > 0)
{
if (!rEMail.IsMatch(textBox1.Text))
{
MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.SelectAll();
e.Cancel = true;
}
}
}



Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Athira Sreejith             
Posted Date: December 31, 2008     Points: 5   

Hi,,

Twi way is ther.

1.RegularExpressionValidator:

<asp:RegularExpressionValidator ID="reg_email" runat="server" ControlToValidate="txtEmailId"
ErrorMessage="Enter valid E-mailId" Display="None" Text="*" ValidationGroup="RegisterValidation"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

2. Javascript:
<script>
function validateEmail(email)
{
var emailpat=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
var matchArray=email.match(emailpat);
if(matchArray==null)
{
return false;
}
return true;
}
function ValidateCtrl()
{
if(document.getElementById("txtemailid").value!="")
{
if(!validateEmail(document.getElementById("txtemailid").value))
{
alert("Invalid Email Address");
document.frmpostresume.txtemailid.focus();
return false;
}
}
}
</script>

Author: Narayanan             
Posted Date: February 07, 2012     Points: 20   


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend