Re: Regular expression to test and limit number of characters



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.
.



Relevant Pages

  • Re: Regular Expression Question
    ... The first replace, doing the heavy lifting, says: eat chars ... followed by three other digits. ... Characters now continue to be eaten starting from the char ...
    (microsoft.public.scripting.vbscript)
  • Re: PDFs: overriding "You cannot save data typed into this form"?
    ... finished very quickly with failure. ... force, so it can be quite time consuming - as I mentioned before, I ... usually do a run of digits only first, then one run with digits and ... long run with all chars or take a chance that it's alphanumeric. ...
    (Ubuntu)
  • Re: Looking for .COM->"ASCII" .COM util
    ... to work entirely within an even smaller character set: digits + ... I.e. for each pair of decoded chars 13 bits pops out: The lower 8 bits are immediately written to the output buffer, while the top 5 bits are combined with any previous 5-bit chunks. ... If the ONLY characters that are required are the 10 decimal ...
    (comp.lang.asm.x86)
  • Regular expression to test and limit number of characters
    ... only one alpha char allowed - rest are digits, ... total length must be between 9 and 12 chars, ... will be I'm having hard time limiting number of digits either before ... What's a good way to enforce above rules. ...
    (comp.lang.javascript)
  • Re: Simple regex question
    ... y is always positive, 1-4 digits ... What do you mean by chars? ... Is any character valid or are only ... a-z, A-Z, and 0-9? ...
    (perl.beginners)