[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regex - check fo only first 2 characters

Posted on 2011-10-11
9
Medium Priority
?
225 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Camillia
  • 5
  • 3
9 Comments
 
LVL 4

Expert Comment

by:safiint
ID: 36948917
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
 
LVL 4

Expert Comment

by:safiint
ID: 36948945
I think you have to modify pattern like this:

^([a-zA-Z]+)([\-0-9]+)$
0
 
LVL 7

Author Comment

by:Camillia
ID: 36949176
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:safiint
ID: 36949462
the pattern you wrote accept:
firstly: alphabet at least one.
secondly: numbers at least one number.
thirdly: any characters
0
 
LVL 7

Author Comment

by:Camillia
ID: 36949574
so it's ok..the one i found??
0
 
LVL 4

Expert Comment

by:safiint
ID: 36950516
yes.
but don't forget
by that pattern the user can put any thing at the last.
he can write like
Are98565555.^%2!@ *()
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36951243
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
 
LVL 7

Author Comment

by:Camillia
ID: 36951320
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
 
LVL 4

Accepted Solution

by:
safiint earned 2000 total points
ID: 36954450
if you want :
2 alpha then 7digits then any characters, use this:
^[a-zA-Z]{2}\d{7}.*$
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question