Solved

Regular Expression help

Posted on 2013-06-06
7
503 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
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!

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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