Solved

Regular Expression help

Posted on 2013-06-06
7
502 Views
Last Modified: 2013-06-06
I am using this regex in my code to validate the length of the entered data before allowing the user to submit the page.

^[a-zA-Z0-9\-\s]{11,23}

The characters can be alphanumeric, but needs to be either 11 or 23 in length. The example above allows anything between 11 and 23. I have been looking for about an hour for an example to do this, but can't seem to get it right.

Can someone help me adjust this so that my users can only enter 11 or 23 characters?
0
Comment
Question by:Jerry Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 21

Accepted Solution

by:
Mazdajai earned 500 total points
ID: 39225675
^([a-zA-Z0-9\-\s]{11})|([a-zA-Z0-9\-\s]{23})$
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39225720
Alternatively:
^[a-zA-Z0-9\-\s]{11}(?:[a-zA-Z0-9\-\s]{12})?$

Open in new window

0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39225730
You mean {23} not {12}?
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 18

Author Comment

by:Jerry Miller
ID: 39225733
I had to put the 23 in the first part of the clause, but other than that it is awesome! I didn't know that I could use the pipe (|) command as an OR in regex.

Any idea why it works like below, but no the other way around? Maybe because with the more restrictive clause in the front it doesn't process the second correctly?

^([a-zA-Z0-9\-\s]{23})|([a-zA-Z0-9\-\s]{11})$

Thanks for the quick help.
0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39225761
I don't see a reason why it would not work, do you have a sample output? You can try kaufmed's as well, both should work.
0
 
LVL 18

Author Comment

by:Jerry Miller
ID: 39225844
I didn't see kaufmed's reply until I submitted my response or I would have tried it too and split the points.

I am using it to validate user input on a textbox in the markup page to make sure they are following established business rules. I am also validating in the code behind, but I am trying to give them visible feedback as they type so it can be corrected as it is entered.

<asp:RegularExpressionValidator ID="txtEntryNumberRegExpValidator" runat="server" ErrorMessage="Entry Number can contain letters & numbers with a hypen (-): 11 or 23 characters"
        ControlToValidate="txtEntryNumber" ValidationExpression="^([a-zA-Z0-9\-\s]{23})|([a-zA-Z0-9\-\s]{11})$" />
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39225856
@Mazdajai
You mean {23} not {12}?
No, I mean 12.  11 + 12 = 23    : )
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

696 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