Solved

Regular Expression help

Posted on 2013-06-06
7
492 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
  • 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 74

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 74

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 101
groovy example issue 10 65
python sqlite question 11 42
Wallpaper not updating when file is swapped out 6 48
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now