?
Solved

Regular Expression help

Posted on 2013-06-06
7
Medium Priority
?
506 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 2000 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
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

 
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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Make the most of your online learning experience.
Introduction to Processes
Starting up a Project
Suggested Courses

752 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