-
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
-
May 10th, 2011, 09:46 AM
#2
Re: textbox email validation
First up, with over 600 posts, we shouldn't have to tell you to use Code or VBCode tags to format your code snippets. That should be an automatic courtesy to make reading your post easier for those whom you would like to volunteer their time to help you.
As for the question, you don't handle the Leave event for validation. You handle the aptly named Validating event. In the event handler, you set e.Cancel to true if the field fails validation. That way it never loses focus and the Leave event is never raised.
It also allows you to set the CausesValidation property of a Button to False so that the user can click that Button to exit without validating, which the Leave event can't do. Finally, it also allows you to validate on an ad hoc basis by calling the form's ValidateChildren method, which the Leave event can't do.
-
May 10th, 2011, 10:34 AM
#3
Thread Starter
Fanatic Member
Re: textbox email validation
There is no validating event.
Note that I am using wpf.
Thanks
-
May 10th, 2011, 06:59 PM
#4
Re: textbox email validation
Originally Posted by arkiboys
There is no validating event.
Note that I am using wpf.
Thanks
Ah, I probably should have realised that from the fact that you're using a RoutedEventArgs but, given that there is a forum dedicated to WPF, if you're going to post here then you should say so.
-
May 11th, 2011, 02:22 AM
#5
Thread Starter
Fanatic Member
Re: textbox email validation
Should I put this into wpf section?
-
May 11th, 2011, 05:24 AM
#6
Re: textbox email validation
I'd suggest so, but don't double post. Use the Report icon to send a message to the mods to ask them to move it.
-
May 11th, 2011, 05:46 AM
#7
Thread Starter
Fanatic Member
Re: textbox email validation
Ok, but would you know how to fix the problems by not using xaml and just using the events?
Thanks
-
May 11th, 2011, 06:34 AM
#8
Re: textbox email validation
Originally Posted by arkiboys
Should I put this into wpf section?
I have moved it for you, and added [code]your code goes here[/code] tags to your first post.
-
May 11th, 2011, 06:47 AM
#9
Thread Starter
Fanatic Member
Re: textbox email validation
-
May 13th, 2011, 02:48 PM
#10
Re: textbox email validation
If the validating function is correct (I wouldn't use a regex) and the problem is the "event" you could take a look at this link: http://stackoverflow.com/questions/3...validation-wpf
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
|