Validate emailaddress
-
How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance
-
How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance
The best way of doing this is using Regex. Try the below code.
private void txtboxEmailAdd\_Validating(object sender, CancelEventArgs e) { string pattern = @"^\[a-z\]\[a-z|0-9|\]\*(\[\_\]\[a-z|0-9\]+)\*
([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
System.Text.RegularExpressions.Match match =
Regex.Match(txtboxEmailAdd.Text.Trim(), pattern, RegexOptions.IgnoreCase);
if (match.Success || txtboxEmailAdd.Text == "")
return;
else
MessageBox.Show("Invalid Email Address" + "\r\n" + "Please enter Valid Email", "Invalid Email Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel = false;
}Excellence is doing ordinary things extraordinarily well.
-
How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance
Use a regular expression. There are several examples of email validation on google - some more accurate than others, so take your time and read around the various implementations (i.e. look for ones that can handle more complex email addresses such as .museum addresses).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance
Didn't read the question properly!
string s = "Hello@email.com";
if (s.IndexOf("@") <= 0)
{
MessageBox.Show("Not an EMAIL address");
}This will report on @email.com and email.com, but not on a@email.com
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Didn't read the question properly!
string s = "Hello@email.com";
if (s.IndexOf("@") <= 0)
{
MessageBox.Show("Not an EMAIL address");
}This will report on @email.com and email.com, but not on a@email.com
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
This is not a workable solution. What happens if the user enters @me, or bob@bob, or ¬¬¬@¬¬¬?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
This is not a workable solution. What happens if the user enters @me, or bob@bob, or ¬¬¬@¬¬¬?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Agreed it is not the complete solution - it covers the @me case since the indexof returns 0 - but I didn't want to confuse him too much, just answer the question as asked. :) A regular expression would work better, but I guess the "proper" solution would be to interface to Outlook (or express, or whatever) and check the entered address against his contacts list - ouch!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Agreed it is not the complete solution - it covers the @me case since the indexof returns 0 - but I didn't want to confuse him too much, just answer the question as asked. :) A regular expression would work better, but I guess the "proper" solution would be to interface to Outlook (or express, or whatever) and check the entered address against his contacts list - ouch!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
Have a look at the RegEx Expression I gave him. It caters for all the validation, checks on the domain etc etc and that there is a valid name@something.com etc. I think I added a check in that there are no spaces in the email address either. I think it is pretty comprehensive in what it checks. Even if I have to say so myself.... ;)
Excellence is doing ordinary things extraordinarily well.
-
Have a look at the RegEx Expression I gave him. It caters for all the validation, checks on the domain etc etc and that there is a valid name@something.com etc. I think I added a check in that there are no spaces in the email address either. I think it is pretty comprehensive in what it checks. Even if I have to say so myself.... ;)
Excellence is doing ordinary things extraordinarily well.
Kwagga wrote:
Even if I have to say so myself....
Smartass :-D - your code earns you a perma link as I'm certain to need this sometime in the future! Thanks
Never underestimate the power of human stupidity RAH
-
Kwagga wrote:
Even if I have to say so myself....
Smartass :-D - your code earns you a perma link as I'm certain to need this sometime in the future! Thanks
Never underestimate the power of human stupidity RAH
-
Have a look at the RegEx Expression I gave him. It caters for all the validation, checks on the domain etc etc and that there is a valid name@something.com etc. I think I added a check in that there are no spaces in the email address either. I think it is pretty comprehensive in what it checks. Even if I have to say so myself.... ;)
Excellence is doing ordinary things extraordinarily well.
The original RFC regex is (as follows):
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
It's not perfect, but is a starting point.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.