Reputation: 23
In ASP.NET Validator, I want to validate a string like this - "/script/www/test/123.45" Basically, ensure that the string should always start with "/" followed by anything. If it does not start with "/" then its an error condition.
Upvotes: 0
Views: 198
Reputation: 24370
The answers before me are great, just adding yet another extremely simple one:
if (path[0] == '/')
Using a regular expression for this would be like using power tools instead of cutlery when dining.
Upvotes: 2
Reputation: 11912
bool isValid(string sInputString)
{
Regex rex = new Regex(@"^/");
return rex.IsMatch(sInputString);
}
Upvotes: 1
Reputation: 120518
if( !path.StartsWith("/") ) throw...
?
Regex seems like overkill.
Upvotes: 4