Validate eMail address using MailAddress.ParseValue

 
We have Regular Expression validator to validate the eMail address, specified by the users. It usually warn user about some common mistakes. But sometimes eMail address passes validation  , but later causes exception in MailAddress.ParseValue called from MailAddress constructor.
I decided to call MailAddress.ParseValue as a part of UI validator, but it's private. (I've asked MS to change it) So I had to open constructor inside try block and catch exceptions in case if format is wrong. Alternatively I could use reflection to call private MailAddress.ParseValue or copy the code from Reflector disassembly or from Mono.
 

            try
            {
                //MailAddress.ParseValue is private
            //            if(MailAddress.ParseValue(txtEmailText)
                //TODO: use Reflector or find some code to validate address as strict as ParseValue , for now just catch Exception
                        MailAddress addr=new MailAddress( txtEmailText);
            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Investigate why ?."+exc.ToString());
                isValid = false;
                sWrongEmailMsg = "<br>This email address should be in the format of name@domain.com";
            }

 Post of Craig Barber in thread The specified string is not in the form required for an e-mail address explains that MS Mail doesn't support a dot, ".", preceding the at-sign, "@", e.g. foobar.@example.com . It is "right" thing according to RFC 2822, but some email providers, including gmail, earthlink, and comcast allow users to create and deliver to addresses in the aformentioned format. The users will only be confused or frustrated.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Monday, February 02, 2009 10:27 PM
Print

Comments on this entry:

No comments posted yet.

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910