Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
once i enter the first character the message box immediately shows invalid email

What I have tried:

if (textBox3.Text.Contains("@"))
{
if (textBox3.Text.Contains(".com"))
{
MessageBox.Show("Valid Email");
}
}
else
{
MessageBox.Show("Invalid Email");
}
Posted
Updated 27-Dec-18 2:26am
Comments
[no name] 27-Dec-18 6:55am    
In case you like/need to verify the adress Format on each key stroke, I would suggest that you don't Show a message box in case it is invalid. Better you use a Label to Show the Status of the user Input.

The problem is that it is right: whatever you type to startwith will not be a valid email.
Think about it: Your email is "abc@def.ghi"
When you type 'a' that isn't valid.
When you add 'b' it still isn't valid.
If only becomes valid when at least two characters have been typed after the '.'!
"abc@def.gh" is technically a valid email, although it probably won't go anywhere useful.

You can't check for a valid email until the user has finished entering it: so either do your check and show a message box when he leaves the textbox (that's what the Validating event[^] is there for), or use a label near the textbox to show "invalid email address" instead.

But do be aware the email addresses can be more complicated than your seem to think: This would pass your validation but not be valid: "a.com@" while these would not: "abc@talktalk.net", "abc.def.co.uk", "abc@usa.gov".

Try using a Regex: regex for valid email - Google Search[^] - it;'s probably the simplest way to do a basic check of "OKness", though the only way to be sure it's a "real address" is to send an email that requires a link to be followed.
 
Share this answer
 
Quote:
once i enter the first character the message box immediately shows invalid email

That is the principle!
An Email address is not valid until it is completed.
One way to handle this is to replace the message box with a color change in input field, make it red as long as address is not valid.

Advice: use RegEx to validate Email address, it ease the check.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
 
Share this answer
 
Yep, that is to be expected. You need to have the full entry before you can validate it. And the tests you have are very rudimentary and limiting.

You can use a RegEx pattern to see if the full address is valid, or relatively close. Email address rules are so convoluted it is near impossible to find one that is 100% correct.
How to: Verify that Strings Are in Valid Email Format | Microsoft Docs[^]

Another method to verify would be use the MailAddress class as a validator/
The theory is this constructor will return exceptions for null, empty, and un-recognized formats. Safe to say that if it throws an error, the address is bad
MailAddress Constructor (System.Net.Mail) | Microsoft Docs[^]
C#
public bool IsValidEmail (string addy) {
  try {
    MailAddress test = new MailAddress(addy);
    return true;
  }
  catch (Exception) { return false; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900