I have some code that checks a fields input against a regex, although for some reason (no matter what I put in the field, it returns flase. Is there something I have missed?
private void textBox5_Validating(object sender, CancelEventArgs e)
{
String AllowedChars = @"^a-zA-Z0-9.$";
if (Regex.IsMatch(textBox5.Text, AllowedChars))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid");
}
}
The regex makes no sense to me. This one would (notice the square brackets used for defining an alphabet):
String AllowedChars = @"^[a-zA-Z0-9]*$";
What you want is to group those characters and allow 0 or more:
@"^[a-zA-Z0-9.]*$"
Otherwise, what you posted allows "a-zA-Z0-9" and one more character only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With