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 11
HuaMin ChenSystem AnalystAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
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
 
Dorababu MSenior Software EngineerCommented:
Why you are not using regular expression to validate the email?
0
 
HuaMin ChenSystem AnalystAuthor Commented:
How to adjust the way?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
HuaMin ChenSystem AnalystAuthor 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 ChenSystem AnalystAuthor 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 ChongCommented:
are you trying to..

1. verify if function IsValidEmail is working, OR
2. the validation of your page seems not working?
0
 
HuaMin ChenSystem AnalystAuthor Commented:
Yes, code is fine but I cannot show the message as expected.
0
 
Ryan ChongCommented:
I don't think I shall take all the credits here as Dorababu did make contribution in his post.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.