textbox email validation-VBForums
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 07:33 AM. Reason: Added Code Tags

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,941

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,941

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,941

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  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
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    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.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  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,586

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.