I was just working on some validation and was stuck up on this though :( I want a text which contains only [a-z][A-Z][0-9][_] .
It should accept any of the above characters any number of times in any order. All other characters marks the text as invalid.
I tried this but it is not working !!
{
......
Regex strPattern = new Regex("[0-9]*[A-Z]*[a-z]*[_]*");
if (!strPattern.IsMatch(val))
{
return false;
}
return true
}
You want this:
Regex strPattern = new Regex("^[0-9A-Za-z_]*$");
Your expression does not work because:
^
and $
characters. This means that every string will match, because every string contains zero or more of the specified characters. (For example, the string "!@#$" contains zero numbers, etc.!) Anchoring the expression to the start and end of the string means that the entire string much match the entire expression or the match will fail.*
near the end of the expression to +
. (*
means "0 or more of the previous token" while +
means "1 or more of the previous token.")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