Karl O'Connor
Karl O'Connor

Reputation: 1575

regular expression in c#

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

Answers (5)

Ahmed Ghoneim
Ahmed Ghoneim

Reputation: 7067

Simply,

Pattern = "^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$"

Details :

  • Start. ^
  • Zero or more mixed alphanumerical. [a-zA-Z0-9]*
  • One or more numerical. [0-9]+
  • Zero or more mixed alphanumerical. [a-zA-Z0-9]*
  • End. $

Upvotes: 0

Joshua
Joshua

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

Subs
Subs

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

Rawling
Rawling

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

Gonzalo.-
Gonzalo.-

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

Related Questions