Validating user input with reg exp in a WPF textbox

I have an array of input checks. Each row in the array represents a single input validation; the regular expression check string and a string to display to the user if the check fails:

public class myClass
{
     public static string[][] inputsInfo = new string[4][];

     static myClass()
     {
     // ID - 9 digits
     inputsInfo[0] = new string[2] { "^[0-9]{9}$", "exactly 9 digits (0-9)" };

     // only letters and possibly more than one word
     inputsInfo[1] = new string[2] { "^[A-Za-z]{2,}(( )[A-Za-z]{2,})*$", "only letters (A-Z) or (a-z)" };

     // Number - unlimited digits
     inputsInfo[2] = new string[2] { "^[0-9]+$", "only digits (0-9)" };

     // username, password
     inputsInfo[3] = new string[2] { "^[A-Za-z0-9]{6,}$", "at least 6 characters.\nOnly letters (A-Z) or (a-z) and digits (0-9) are allowed" };

     }
..............
..............
}

      

I have windows containing WPF textboxes. There are fields that have the same input validation, and so I want to store all the input validations in an array, so I can just select the validation I want at the moment.

I have this form:

...............

        <TextBlock Grid.Row="2" Grid.Column="0" Text="First name"/>
        <TextBox x:Name="firstName" Grid.Row="2" Grid.Column="1"/>
        <Button Grid.Row="2" Grid.Column="2" Content="Search"/>

        <TextBlock Grid.Row="3" Grid.Column="0" Text="Last name"/>
        <TextBox x:Name="lastName" Grid.Row="3" Grid.Column="1"/>
        <Button Grid.Row="3" Grid.Column="2" Content="Search"/>

        <TextBlock Grid.Row="4" Grid.Column="0" Text="ID number"/>
        <TextBox x:Name="ID" Grid.Row="4" Grid.Column="1"/>
        <Button Grid.Row="4" Grid.Column="2" Content="Search"/>

...............

      

Each text box has a button next to the Click event. How can I perform input validation on a button click?

Is there a way to do this using XAML code? or only in code with C # code?

Any help would be appreciated.

+3


source to share


1 answer


How can I perform input validation on a button click?

Why not create boolean flags in the ViewModel that track the property of the bound textbox of the target for validation. Example

VM:

public string FirstName { get { return _firstName; }
                          set { _firstname = value; 
                                PropertyChanged("IsFirstNameValid");
                                PropertyChanged("FirstName");
                              }
                        }

public bool IsFirstNameValid { get { return Regex.IsMatch( FirstName,
                                                           ValidationPatternFirstName); }}

      



XAML

<TextBox x:Name="firstName" 
         Text={Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />

      

Then, whenever the stored value changes, the FirstName

boolean value IsFirstNameValid

will accurately reflect that status on subsequent access.

Also you can snap to IsFirstNameValid

on screen to show the icon or not and it will be updated according to its status.

0


source







All Articles