I am trying to prevent users entering '%' or multiple '%'s in to a HTML form in .NET program. Tried pattern (?!.%*) but it's not working. Regex should allow '%' in the string but should throw an error when only '%' are used.
I'm not a regex expert but this seems to be working for me:
bool IsInputValid(string input)
{
return !Regex.IsMatch(input,"^[%]+$");
}
And an example:
Debug.WriteLine(IsInputValid("%")); // False
Debug.WriteLine(IsInputValid("%%")); // False
Debug.WriteLine(IsInputValid("fsd%lkf")); // True
Debug.WriteLine(IsInputValid("%fslk")); // True
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