Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Regex - block '%'

Tags:

.net

regex

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.

like image 601
NetNewBie Avatar asked Apr 22 '26 08:04

NetNewBie


1 Answers

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
like image 171
Nasreddine Avatar answered Apr 28 '26 05:04

Nasreddine