locked
textbox email validation RRS feed

  • Question

  • 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();
          }
        }
    
    Tuesday, May 10, 2011 12:34 PM

Answers

  • Hello,

     

    Try using the PreviewLostKeyboardFocus routed event and setting Handled = true in the event handler:

            <TextBox Name="txtEmail" PreviewLostKeyboardFocus="txtEmail_LostFocus"></TextBox>

            private void txtEmail_LostFocus(object sender, RoutedEventArgs e)
            {
                if (IsEmailAllowed(txtEmail.Text.Trim()) == false)
                {
                    e.Handled = true;
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtEmail.Focus();
                }
            }

     

    Hope it helps.

    • Marked as answer by Min Zhu Tuesday, May 17, 2011 2:46 AM
    Tuesday, May 10, 2011 1:02 PM

All replies

  • Hello,

     

    Try using the PreviewLostKeyboardFocus routed event and setting Handled = true in the event handler:

            <TextBox Name="txtEmail" PreviewLostKeyboardFocus="txtEmail_LostFocus"></TextBox>

            private void txtEmail_LostFocus(object sender, RoutedEventArgs e)
            {
                if (IsEmailAllowed(txtEmail.Text.Trim()) == false)
                {
                    e.Handled = true;
                    MessageBox.Show("E-Mail expected", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtEmail.Focus();
                }
            }

     

    Hope it helps.

    • Marked as answer by Min Zhu Tuesday, May 17, 2011 2:46 AM
    Tuesday, May 10, 2011 1:02 PM
  • Hi Arkiboys,

    We have not heard from you in a couple of days.

    Please post back at your convenience if we can assist further.

    Enjoy your day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 12, 2011 6:09 AM
  • Hi Arkiboys,

    I think HomeroThompson's suggestion could work. I marked it as answer. If you have any new questions or concerns about this issue, please feel free to let me know.

    Thank you and have a nice day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 2:48 AM