Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need a simple way to validate of text boxes (Required Field). It should check all mandatory field existence , when user press button.

What I have tried:

HTML
<Window.Resources>
     <ControlTemplate x:Key="validationTemplate">
                <DockPanel>
                    <TextBlock Foreground="Red" FontSize="25" Text="*" DockPanel.Dock="Right"></TextBlock>
                    <AdornedElementPlaceholder/>
                </DockPanel>
            </ControlTemplate>
        </Window.Resources>
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Height="26" Margin="62,213,0,0" VerticalAlignment="Top" Width="121" Click="Button_Click_1"/>
    <TextBox x:Name="txtEmail1" Text="" Height="61" Margin="116,10,194,0" Validation.ErrorTemplate="{StaticResource validationTemplate}"/>
</Grid>
Posted
Updated 12-Mar-17 10:25am

1 solution

You maybe need to check if text string is empty , and if it isnt validate via regex

C#
if(txtEmail1.Text.Equals(string.empty))
{
   MessageBox.Show("Text is required");
}
else
{
   // check email validation with regex
   Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
   Match match = regex.Match(email);
   if (match.Success)
   {
       // email is correct , write here your to do code
   }
   else
   {
    MessageBox.Show("Email is not correct ");
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900