Results 1 to 10 of 10

Thread: textbox email validation

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: textbox email validation

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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: textbox email validation

    Quote Originally Posted by arkiboys View Post
    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: textbox email validation

    Should I put this into wpf section?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: textbox email validation

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

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: textbox email validation

    Quote Originally Posted by arkiboys View Post
    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.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: textbox email validation

    ok, thanks

  10. #10
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    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
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width