Re: Regular expression to test and limit number of characters
- From: Dr J R Stockton <jrs@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Feb 2008 18:17:38 +0000
In comp.lang.javascript message <7c5eb436-743c-4028-ba34-73cebeee4595@o7
7g2000hsf.googlegroups.com>, Wed, 27 Feb 2008 14:29:03, Sharkie
<sharkdba@xxxxxxxxx> posted:
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.
1) OK = St.length >= 9 && St.length <= 12 && /^\d*[A-Z]\d*$/.test(St)
2) OK = /^\d*[A-Z]\d*$/.test(St) && /^.{9,12}$/.test(St)
3) OK = /^\d*[A-Z]\d*$&^.{9,12}$/.test(St)
^ AFAIK, not implemented - but maybe desirable?
Opera now 9.26.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borland.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care.
.
- References:
- Prev by Date: Re: Don't understand flow using xmlhttp
- Next by Date: Re: JScript XSL problems
- Previous by thread: Re: Regular expression to test and limit number of characters
- Next by thread: Re: Regular expression to test and limit number of characters
- Index(es):
Relevant Pages
|