[Javascript] Interesting validation problem

Paul Novitski paul at novitskisoftware.com
Sun May 1 18:10:52 CDT 2005


At 07:12 AM 5/1/2005, David Lovering wrote:
>I've been presented with a problem that requires "real-time" validation, 
>namely testing input submitted into a field character-by-character as it 
>is entered, and then "bleeping" and rejecting the character(s) which don't 
>match a regex criteria.  This is particularly helpful when a long, 
>complicated input is solicited as it saves the person from having to guess 
>where they went wrong (or worse yet, retyping the whole thing over again).
...

Dave,

I don't know what your actual application is, but it seems to me that you 
should only need two regular expressions: one for testing partial strings 
and one for the complete input.

My regexp skills are rusty and this might not be correct, but I'm thinking 
of something like this to test the last seven digits of a North American 
telephone number:

         ^\d{1,3}($|(-\d{0,4}))$

by which I mean:  "From 1 to 3 digits followed by either the end of string 
OR a hyphen followed by 0 to 4 digits... all followed by the end of 
string."  (I imagine Shawn and others more regexp-enabled than myself can 
improve on that to eliminate the double reference to the end of string.)

Wouldn't that test true at every stage of input until the number's complete?

Then to test the complete number:

         ^\d{3}-\d{4}$

Paul  





More information about the Javascript mailing list