Problem to detect issue of Email address

Hi,
Using following codes, I cannot detect problem to Email address. Why?
                <asp:TextBox ID="tb_email"
                    Width="310px"
                    Enabled="false"
                    AutoPostBack="true"
                    OnTextChanged="email_post_change"
                    Font-Names="標楷體" 
                    Font-Size="13pt" 
                    Font-Bold="true"
                    ForeColor="Black"
                    runat="server" />
					...
        bool IsValidEmail(string email)
        {
            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address == email;
            }
            catch
            {
                return false;
            }
        }
        protected void email_post_change(object sender, EventArgs e)
        {
            string email0 = tb_email.Text;
            if (!IsValidEmail(email0))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "ShowMessage('电子邮件地址格式有问题。')", true);
                tb_email.Focus();
                return;
            }
        }
		...

Open in new window

LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dorababu MSenior Software EngineerCommented:
Why you are not using regular expression to validate the email?
0
HuaMin ChenProblem resolverAuthor Commented:
How to adjust the way?
0
Dorababu MSenior Software EngineerCommented:
<asp:TextBox ID="tb_email"
                    Width="310px"
                    Enabled="false"
                    Font-Names="標楷體" 
                    Font-Size="13pt" 
                    Font-Bold="true"
                    ForeColor="Black"
                    runat="server" />

<asp:RegularExpressionValidator ID="validateEmail"    
  runat="server" ErrorMessage="Invalid email."
  ControlToValidate="tb_email" 
  ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" />

Open in new window

0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

HuaMin ChenProblem resolverAuthor Commented:
Thanks. How to achieve the same (in the codes) as

ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"

?
0
Dorababu MSenior Software EngineerCommented:
You can use the same in your validate function
 Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);
            if (match.Success)
           {
                 //valid email
           }
          else
          {
               //invalid
          }

Open in new window

0
HuaMin ChenProblem resolverAuthor Commented:
Hi,
Thanks. Using these codes
        private string DomainMapper(Match match)
        {
            // IdnMapping class with default property values.
            IdnMapping idn = new IdnMapping();

            string domainName = match.Groups[2].Value;
            try
            {
                domainName = idn.GetAscii(domainName);
            }
            catch (ArgumentException)
            {
                invalid = true;
            }
            return match.Groups[1].Value + domainName;
        }
        public bool IsValidEmail(string strIn)
        {
            invalid = false;
            if (String.IsNullOrEmpty(strIn))
                return false;

            // Use IdnMapping class to convert Unicode domain names.
            try
            {
                strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
                                      RegexOptions.None, TimeSpan.FromMilliseconds(200));
            }
            catch (RegexMatchTimeoutException)
            {
                return false;
            }

            if (invalid)
                return false;

            // Return true if strIn is in valid e-mail format.
            try
            {
                return Regex.IsMatch(strIn,
                      @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                      @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
                      RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
            }
            catch (RegexMatchTimeoutException)
            {
                return false;
            }
        }
        protected void email_post_change(object sender, EventArgs e)
        {
            string email0 = tb_email.Text;
            if (!IsValidEmail(email0))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "ShowMessage('电子邮件地址格式有问题.')", true);
                tb_email.Focus();
                return;
            }
        }

Open in new window

I still cannot capture error to Email address (field below) of this URL http://my-friend.co/UserMaintenance2/Default.aspx?userid=new&readonly=n

20y.png
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
are you trying to..

1. verify if function IsValidEmail is working, OR
2. the validation of your page seems not working?
0
HuaMin ChenProblem resolverAuthor Commented:
Yes, code is fine but I cannot show the message as expected.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
have you tested what Dorababu posted in ID: 42442295?

in general, it should worked well if removing Enabled="false" or after you dynamically enabled that textbox field:

<asp:TextBox ID="tb_email"
                    Width="310px"
                    Enabled="false"
                    Font-Names="標楷體"
                    Font-Size="13pt"
                    Font-Bold="true"
                    ForeColor="Black"
                    runat="server" />

<asp:RegularExpressionValidator ID="validateEmail"    
  runat="server" ErrorMessage="Invalid email."
  ControlToValidate="tb_email"
  ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" />

SnapShot.png
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
I don't think I shall take all the credits here as Dorababu did make contribution in his post.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.