-
May 10th, 2011, 07:33 AM
#1
Thread Starter
Fanatic Member
textbox email validation
Hi,
In c#, I would like to validate the txtEmail by checking if a valid email address has been entered before moving away from the textbox.
If email is not valid then, txtemail should get the focus again.
The code below is what I have for this purpose but whether or not the email is valid the messagebox appears constantly and does not go away.
CAn you please see what is wrong?
Thanks
Code:
private static bool IsEmailAllowed(string text)
{
bool blnValidEmail = false;
Regex regEMail = new Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (text.Length > 0)
{
blnValidEmail = regEMail.IsMatch(text);
}
return blnValidEmail;
}
private void txtEmail_LostFocus(object sender, RoutedEventArgs e)
{
if (IsEmailAllowed(txtEmail.Text.Trim()) == false)
{
MessageBox.Show("E-Mail expected", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
txtEmail.Focus();
}
}
Last edited by Hack; May 11th, 2011 at 06:33 AM.
Reason: Added Code Tags
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|