Geeks With Blogs

News My Blog has been MOVED to
Michael Freidgeim's OLD Blog My Blog has been MOVED to
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.

                //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";

 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. . 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.

Posted on Monday, February 2, 2009 10:27 PM .Net Framework | Back to top

Comments on this post: Validate eMail address using MailAddress.ParseValue

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Michael Freidgeim | Powered by: