# .NET and More > WPF, WCF, WF >  textbox email validation

## arkiboys

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



```
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();
            }
        }
```

----------


## jmcilhinney

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.

----------


## arkiboys

There is no validating event.
Note that I am using wpf.
Thanks

----------


## jmcilhinney

> 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.

----------


## arkiboys

Should I put this into wpf section?

----------


## jmcilhinney

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.

----------


## arkiboys

Ok, but would you know how to fix the problems by not using xaml and just using the events?
Thanks

----------


## Hack

> 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.

----------


## arkiboys

ok, thanks

----------


## Lightning

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

----------

