problem to validate email address RRS feed

  • Question

  • Hi All.

    I'm trying to validate email address in the text field

    <xctk:WatermarkTextBox x:Name="EmailTextBox" Watermark="Email Address" VerticalContentAlignment="Center" 
    Grid.Column="1" Grid.Row="3" TabIndex="3" Height="30" TextChanged="EmailTextBox_TextChanged"
    LostFocus="EmailTextBox_LostFocus" > <Binding Path="EmailAddress" /> </xctk:WatermarkTextBox> <TextBlock x:Name="errEmail" Text="" FontSize="10" Foreground="#FFFF0000" Margin="5" Grid.Column="1" Grid.Row="3"
    VerticalAlignment="Top" TextAlignment="Right" />

    On online I found that code

    bool IsValidEmail(string email)
        try {
            var addr = new System.Net.Mail.MailAddress(email);
            return addr.Address == email;
        catch {
            return false;

    How to apply this code for LostFocus event method?


    Sunday, April 12, 2020 1:04 AM

All replies

  • Try using the PreviewLostKeyboardFocus routed event and setting Handled = true in the event handle


      <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);

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Sunday, April 12, 2020 5:50 PM
  • Hi zleug
    This forum is discussing and asking questions about the Windows Forms such as Winforms controls, libraries, samples, publication and installation.
    Based on your description, it is mostly related to WPF. So it is recommended to ask the questions in Microsoft Q&A forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 13, 2020 1:21 AM