Hi All,
I am new to ASP.Net and web development.
Question
I have a web form in that I have a button and a textbox control which accepts a email address. When I click on the button I want to validate the email address i.e., the email should be like this [email protected]
where somename can be any text or number or combination of both. And this somename can start with underscore/number/alphabets and can have dot in the middle and the email address should not start with otherthan underscore/number/alphabets and should not contain any special symbols(as in all providers such as yahoo,gmail,rediff,etc)
someprovider - like yahoo,google,etc
How to accomplish this
please help
Thanks
sachi
Loading
keshav singhPosted Nov 16, 2009, 12:40 AM
just try this code:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" Display="Static" runat="server" EnableViewState="False" ErrorMessage="please enter valid Email" ControlToValidate="txtemailid"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
Purushottam RathorePosted Nov 12, 2009, 6:59 AM
Try this
<asp:TextBox ID="TextBox1" runat="server" ToolTip="Enter your Email here">asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" EnableViewState="False" Display="Static" ControlToValidate="TextBoxEmailId" ErrorMessage="Email must not be empty" SetFocusOnError="True"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" Display="Static" runat="server" EnableViewState="False" ErrorMessage="Invalid Email" ControlToValidate="TextBoxEmailId"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" SetFocusOnError="True"/>
<asp:Button ID="Button1" runat="server" Text="Check valid Email">asp:Button>
Plz Mark if this post is helpful for you.
EfranPosted Nov 6, 2009, 10:17 AM
the easiest and - at the same time - most powerful way to validate an e-mail address is to use a ready made component which can perform the validation for you. Why bother with all of those details just to validate some addresses? ;)
In our company web site we use EmailVerify.NET, which is a managed component which supports syntax checking, domain MX test and mailbox validation too.
From their home page:
EmailVerify.NET is a powerful .NET component which verifies email addresses using various techniques, including:
It is completely written in managed code (C#) and is compliant with the Common Language Specification (CLS), so it can be used with any other .NET language, including VB.NET, C++/CLI, J#, IronPython, IronRuby and F#.
The price for this component is very small (less than 50 bucks) and their support is one of the greatest I've ever found for a small software company like they are.
Hope this helps.
Kirtan PatelPosted Oct 29, 2009, 9:04 AM
1) using RegEx
using System.Text.RegularExpressions;
protected void Button1_Click(object sender, EventArgs e)
{
string Email = TextBox1.Text.Trim();
Regex reg = new Regex("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b");
if(reg.IsMatch(Email) == true)
{
Response.Write("Email Valid !!");
}
else
{
Response.Write("Email InValid !!");
}
}
2) Or Using MailAddressClass
you can also use MailAddress Class While taking Email Address .. it throws exception if you enter invalid Email address as string in it
like blow code you can use it
using System.Net.Mail;
{
string Mail = new MailAddress("MailAddressHere").ToString();
}
catch(Exception ex)
{
Response.Write("Email InValid !!");
}
Now In Mail Address If you pass invalid Email Address it will throw exception
if it helps you please check "Do you like this answer" please :)
Karthika PalaniswamyPosted Oct 29, 2009, 8:54 AM
hi sachi,
From ToolBox, just drag and drop 'Regular Expression Validator' under Validation node in to your web form. then, just right click 'Regular Expression Validator' go to Properties and click it, now you will find out 'ControlToValidate' in to that give your TextboxID name(eg.TextBox1.Text - this is defult name), and in 'ErrorMessage' write * or "pls..Enter correct EmailID" and then come to 'Validation Expression', just click noe you will see the ''Regular Expression Editer' scroll down it, now you will see the 'Internet EMail Address' option just click, then give ok and close property window. press F5 now check your text box with incorrect emailid it wiil give error message.
If you find this answer is helpfull to you...Please check the check box appear in the top of this answer, it will give pionts to me. :)
naura paxPosted Oct 29, 2009, 8:16 AM
you will find an example : \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
all you have to do is set above string in the ValidationExpression property.
that's all.
please try the solution at least once before raising doubt i would be really thankful.
sachi vasishtaPosted Oct 29, 2009, 8:04 AM
friend could you please show how to use regular expressions with an example related to my problem.
naura paxPosted Oct 29, 2009, 7:53 AM
naura paxPosted Oct 29, 2009, 7:47 AM
you just have to add a ValidationControl and set Causes Validation to true for save button.
You can use RegulareExpressionValidator in which you can set regular expression in the property ValidationExpressionas you described above. (regulare expressions are easily available on web).
eg: \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*