I'm making a regex that accepts an input with any decimal(0-9), +, * or # but shouldn't accept any letters(a-z).
so numbers like
should be accepted.
The regex is invalid when there is any letter in the string.
This is the regex I have so far:
private const string PhoneNumberRegex = "((\\d)|(\\*)|(\\#)|(\\+))?";
private bool IsValid(inputString)
{
// Accept * # + and number
Match match = Regex.Match(inputString, PhoneNumberRegex, RegexOptions.IgnoreCase);
return match.Success;
}
But this regex also returns true on #192#abbef
How can I fix this?
You can use this:
private const string PhoneNumberRegex = @"^[0-9*#+]+$";
Where ^
and $
are anchors for start and end of the string.
Note: RegexOptions.IgnoreCase
is not needed.
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