Email Validation in wpf

Email Validation in WPF.

<Window x:Class="Login_WPF.Registration"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Registration" Height="387" Width="528" Background="Black">
    <Grid  Height="350" Width="525" Background="Bisque">
      <TextBlock Height="20" HorizontalAlignment="Left" x:Name ="errormessage" VerticalAlignment="Top" Width="247" Margin="67,0,0,0" OpacityMask="Crimson" Foreground="#FFE5572C" />

      <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,80,0,0" Name="textBlockEmailId" Text="EmailId" VerticalAlignment="Top" Width="110" />

       <TextBox Height="23" HorizontalAlignment="Left" Margin="183,80,0,0" Name="textBoxEmail" VerticalAlignment="Top" Width="222" />

  <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="183,204,0,0" Name="Submit" VerticalAlignment="Top" Width="70" Click="Submit_Click" />

      </Grid>
</Window>

Add RegularExpressions namespace like as follows

using System.Text.RegularExpressions;

private void Submit_Click(object sender, RoutedEventArgs e)
{
    if (textBoxEmail.Text.Length == 0 || passwordBox1.Password.Length == 0)
    {
        errormessage.Text = "Enter an email.";
        textBoxEmail.Focus();
    }
    else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
    {
        errormessage.Text = "Enter a valid email.";
        textBoxEmail.Select(0, textBoxEmail.Text.Length);
        textBoxEmail.Focus();
    }
 }

 

Ebook Download
View all
Learn
View all