I am trying to check the user input, I want the phone number to be like 000-0000.
But my code here does not work as I want. I input
"123-4444444"
and it also matches.
I am not familiar with Regex, but I need to use it, so can you tell me how to change the pattern?
Regex regex = new Regex(@"\d{3}-\d{4}");
if (!(regex.IsMatch(txtUpdatePhoneNumber.Text)))
{
MessageBox.Show("Phone number format should be 000-0000", "Error");
}
>Solution :
You have to specify start and beginning
Please check the regex here: https://regex101.com/r/MZ4YPo/1
^: start of string
$: end of string
^\d{3}-\d{4}$
Regex regex = new Regex(@"^\d{3}-\d{4}$");
if (!(regex.IsMatch(txtUpdatePhoneNumber.Text)))
{
MessageBox.Show("Phone number format should be 000-0000", "Error");
}