2

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.

5
  • "to allow numbers and letter(s) but not letters" ?? Did you mean not letters alone? Commented Jun 7, 2012 at 11:47
  • I guess , it must be alphanumeric ? Commented Jun 7, 2012 at 11:49
  • @frenchie I think he means "to allow numbers and letter(s) but not only letters" Commented Jun 7, 2012 at 11:49
  • Numbers and letters in any combination, or a sequence of numbers followed by one or more letters per your example? At any rate, look up the | (alternative) operator in regexes. Commented Jun 7, 2012 at 11:50
  • 1
    Your problem is underspecified. Commented Jun 7, 2012 at 11:53

5 Answers 5

6

^[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.)

Sign up to request clarification or add additional context in comments.

3 Comments

I'd take out the \d in the first []. Depending on how the regex engine works, that might improve performance.
@CodeInChaos Cheers, that should work. Took me a minute to get my head around it.
@Goran Well, the question is still open to interpretation... but thanks anyway :)
0

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)

Comments

0

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

1 Comment

thanks for all your replies - joshuams answer seems to have done the trick, i'll confirm and mark as answered, thanks again
0

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.

10 Comments

Shouldn't you add start and end specifiers?
This won't allow a value that starts with a letter... but that may well be what Karl wants, it's a bit hard to tell.
@Rawling Yes it will allow a value that starts with a letter.
Based on my interpretation of the question it's still wrong. It doesn't match A1B2. But who knows what the OP wants.
Well, at least the OP has two problems now :)
|
0

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. $

5 Comments

No, it accepts letters and numbers mixed string or numbers only sting
A letter only string is a special case of a mixed letter-number string, and will be matched by ^[a-zA-Z0-9]+$
The |^[0-9]+$ part is completely useless now. And your code looks like it might suffer from performance issues, at least it's non trivial to optimize this for a regex engine. Compare it with Rawling's code.
I'm not saying that your code is wrong, only that it might be slow for some inputs, compared with Rawlings updated code.
So, let's wait for the optimized one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.