473,657 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expression to test and limit number of characters

I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789 "

I can enforce the first two rules by using this:

/^\d*[A-Z]\d*$/

but not limit chars between 9 and 12. The following doesn't work:

/^(\d*[A-Z]\d*){9,12}$/

because of the asterisk which can be any numbers of chars. Since I
don't know where the alpha character
will be I'm having hard time limiting number of digits either before
or after it.

What's a good way to enforce above rules. This should ideally be one
reg exp.
Feb 27 '08 #1
8 23242
Sharkie wrote:
I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789 "
/^\d{8,11}$/.test(str.repla ce(/[A-Z]/,''))&&/[A-Z]/.test(str)

--
Bart
Feb 28 '08 #2
Bart Van der Donck wrote on 28 feb 2008 in comp.lang.javas cript:
Sharkie wrote:
>I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789 "

/^\d{8,11}$/.test(str.repla ce(/[A-Z]/,'')) && /[A-Z]/.test(str)
"only one alpha char allowed" != "must have one and only one alpha char"

/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 28 '08 #3
Evertjan. wrote:
Bart Van der Donck wrote on 28 feb 2008 in comp.lang.javas cript:
>/^\d{8,11}$/.test(str.repla ce(/[A-Z]/,'')) && /[A-Z]/.test(str)

"only one alpha char allowed" != "must have one and only one alpha char"

/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2
Looking at his regex attempts, I think the original poster meant "must
have one and only one alpha char".

--
Bart
Feb 28 '08 #4
Evertjan. wrote:
/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2
Identical, but more terse:

/^\d{9,12}$/.test(str.repla ce(/[A-Z]/,1))

--
Bart
Feb 28 '08 #5
Bart Van der Donck wrote:
Evertjan. wrote:
>/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.repla ce(/[A-Z]/,1))
Where does the (optional or mandatory) letter fit in the first RegExpr?
--
Rob
Feb 28 '08 #6
RobG wrote:
Bart Van der Donck wrote:
>Evertjan. wrote:
>>/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.repla ce(/[A-Z]/,1))

Where does the (optional or mandatory) letter fit in the first RegExpr?
Ah, OK, I got it - the replace takes care of it first. I'm a bit slower
than usual tonight.

--
Rob
Feb 28 '08 #7
pr
Sharkie wrote:
I need a regular expression to evaluate a text input field. The
validation rules are:

1) only A-Z and 0-9 chars allowed,
2) only one alpha char allowed (at any position) - rest are digits,
3) total length must be between 9 and 12 chars,

a valid example would be "123A456789 "
[...]
What's a good way to enforce above rules. This should ideally be one
reg exp.
For fun only, here's a version that uses a single expression:

/^(\d(?=\d*[A-Z])|[A-Z](?!.*[A-Z])|\d(?=\d*$)){9 ,12}$/

or if the single alpha character is optional:

/^(\d|[A-Z](?!.*[A-Z])){9,12}$/

Not something you would want to use in public.
Feb 28 '08 #8
RobG wrote on 28 feb 2008 in comp.lang.javas cript:
RobG wrote:
>Bart Van der Donck wrote:
>>Evertjan. wrote:

/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.repla ce(/[A-Z]/,1))

Where does the (optional or mandatory) letter fit in the first RegExpr?

Ah, OK, I got it - the replace takes care of it first. I'm a bit slower
than usual tonight.
I like Bart's solution.
That's what the joy of programming is about.

[But it does not cover the mandatoriness of an upperalpha! ;-) ]

An alternative not using test():

!str.replace(/[A-Z]/,1).replace(/\d{9,12}/,'')

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 28 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4167
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
10
2205
by: Jeff Sandler | last post by:
I have a page that accepts input from many textboxes. Many of the textboxes are intended to accept dates and times, thus, I expect only digits to be entered. I originally tested using parseInt and isNaN, but I'm not even sure that the results are as perfect as I need. I am expecting to use RegExp.test(string), but I'm not 100% sure about that, either. Here is a test program with a textbox that has a maxlength of 2 characters. The...
9
3131
by: Mike P | last post by:
I have a regular expression that I use on text boxes where I want to limit the user to letters a-z and spaces. I now need to allow characters such as ö, ä and å (Nordic characters). Does anybody know how to do this in a regular expression? Here is my current standard regular expression : ^+$ Any assistance would be really appreciated.
5
477
by: tmeister | last post by:
I am in need of a regular expression that tests and fails if there are 14 or more of a character in the test string. There can be up to 13 of these characters in the string and any other characters, but at the 14th of this character it should fail. Thanks, Todd Meister
2
3012
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and otherwise ignored. I decided to use the following regular expression to remove all punctuation (actually...
7
12940
by: Tizzah | last post by:
What is wrong with that? regex = /^(http|https):\/\/+({1}+)*\.{2,5}(({1,5})?\/.*)?$/ if(field.hpage.value != regex.test(field.hpage.value)){ alert("Bad Homepage") field.hpage.focus() field.hpage.select() return false
3
2562
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
6
2855
by: rorymo | last post by:
I have a regular expression that allows only certain characters to be valid in an xml doc as follows: <xs:pattern value="^*" /> What I want to do is also allow any unicode character that is enclosed in single quotes to also be valid, no matter where they appear. I tried the following: <xs:pattern value="^*('*)*" />
7
11328
by: Sharkie | last post by:
I need a regular expression which will evaluate to false if number of consecutive characters (non-whitespace) exceeds certain number (10 in this example). For example, I have this function: function test() { var sValue="short unusuallyLongAndWayTooLongString short2"; var regEx=/\S{10,}/; return regEx.test(sValue);
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.