I have multiple textboxes on my windows form, and 1 button that checks if the information inside the textbox is valid. To validate each textbox I'm using Regular Expression.
Example of my textboxes:
- First_Name_textbox
- Phone_Number_textbox
- Date_Of_Birth_textbox
- Credit_Card_textbox etc...
Goal: Instead of writing a bunch of if-else validation statements for each textbox like this example below. How can I check all the textboxes and display a message in a more neater way? Neater meaning less code. I find that many if-else statements can make the code messy.
Example of error messages:
- First Name was not correct
- Phone Number was not correct
- Birthday is not in correct format
etc...
if(FirstName_Regex.IsMatch(First_Name_textbox.Text))
{
}
else
{
MessageBox.Show("Invalid first name");
}
if(PhoneNumber_Regex.IsMatch(Phone_Number_textbox.Text))
{
}
else
{
MessageBox.Show("Invalid Phone Number");
}
if(Credit_Regex.IsMatch(Credit_Card_textbox.Text))
{
}
else
{
MessageBox.Show("Invalid Credit Card informationr");
}