Reputation: 1575
I am trying to validate a texbox to allow numbers and letter(s) but not letters alone only e.g. 13492M
I am using C# regular expressions.
Upvotes: 2
Views: 179
Reputation: 7067
Simply,
Pattern = "^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$"
Details :
^
[a-zA-Z0-9]*
[0-9]+
[a-zA-Z0-9]*
$
Upvotes: 0
Reputation: 4159
This Regex should work fine:
^[A-Za-z]*[0-9]+[A-Za-z]*$
This regex will allow numbers or letters+numbers. Just letters will fail.
Upvotes: 0
Reputation: 529
How about this:
([0-9]+[a-zA-Z]+ | [a-zA-Z]+[0-9]+)[a-zA-Z0-9]*
(Numbers first and then alphabets OR Alphabets first then numbers) atleast once or more then both alphabets and numbers which is optional
Upvotes: 0
Reputation: 50184
^[A-Za-z]*\d[A-Za-z\d]*$
should do it. (Possibly some letters, then a digit, then any more letters or digits.)
(Edited to add start/end matches.)
Upvotes: 6
Reputation: 12682
use maskedTextBox, it use a property "mask" to validate with the Expression that you want. So you only add the RegEx to your maskedTextBox and you don't have to validate everytime in your code (it will check against your RegEx automatically)
Upvotes: 0