Peter Chan
asked on
Problem to detect issue of Email address
Hi,
Using following codes, I cannot detect problem to Email address. Why?
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;
}
}
...
Why you are not using regular expression to validate the email?
ASKER
How to adjust the way?
<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})+)$" />
ASKER
Thanks. How to achieve the same (in the codes) as
ValidationExpression="^([\ w\.\-]+)@( [\w\-]+)(( \.(\w){2,3 })+)$"
?
ValidationExpression="^([\
?
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
}
ASKER
Hi,
Thanks. Using these codes
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;
}
}
I still cannot capture error to Email address (field below) of this URL http://my-friend.co/UserMaintenance2/Default.aspx?userid=new&readonly=n
are you trying to..
1. verify if function IsValidEmail is working, OR
2. the validation of your page seems not working?
1. verify if function IsValidEmail is working, OR
2. the validation of your page seems not working?
ASKER
Yes, code is fine but I cannot show the message as expected.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't think I shall take all the credits here as Dorababu did make contribution in his post.