Regex - check fo only first 2 characters

I have order numbers that start with 2 alpha characters. I use this
 if (Regex.IsMatch(ID, @"^([a-zA-Z\-0-9]*)$"))

but that checks for any combination...it accepts order numbers like
1Z46R28Y9094535616

But I want it to only accept order numbers like : WR00009-1 OR WR00009
LVL 8
CamilliaAsked:
Who is Participating?
 
safiintCommented:
if you want :
2 alpha then 7digits then any characters, use this:
^[a-zA-Z]{2}\d{7}.*$
0
 
safiintCommented:
try this

this pattern accept any character that starts with at least on alpha:
 if (Regex.IsMatch(ID, @"^([a-zA-Z]+)([a-zA-Z\-0-9]*)$"))

this pattern accept any character that starts with exactly 2 alpha:
 if (Regex.IsMatch(ID, @"^([a-zA-Z]{2})([a-zA-Z\-0-9]*)$"))
0
 
safiintCommented:
I think you have to modify pattern like this:

^([a-zA-Z]+)([\-0-9]+)$
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CamilliaAuthor Commented:
I found this online...seems to work ^[A-Za-z]+\d+.*$

any issues with the one i have that you guys can think of?
0
 
safiintCommented:
the pattern you wrote accept:
firstly: alphabet at least one.
secondly: numbers at least one number.
thirdly: any characters
0
 
CamilliaAuthor Commented:
so it's ok..the one i found??
0
 
safiintCommented:
yes.
but don't forget
by that pattern the user can put any thing at the last.
he can write like
Are98565555.^%2!@ *()
0
 
Terry WoodsIT GuruCommented:
If you really want to completely restrict the format, give us the exact number of numbers you want to allow (before, and after the dash, when there is one). It's ok if there's a defined range of lengths too.

eg
 if (Regex.IsMatch(ID, @"^[a-zA-Z]{2}[0-9]{5}(-\d{1,3})?$"))
requires exactly 2 alpha characters, then exactly 5 digits, then an option of a [dash and 1 to 3 more digits]
0
 
CamilliaAuthor Commented:
it will be like this : xx0000134 OR xx0000134-1
Any number can be after the dash. But I want excatly 2 alpha and that can be with or without dash
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.