[Javascript] Interesting validation problem

David Lovering dlovering at gazos.com
Sun May 1 09:12:38 CDT 2005


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

The solution I've used up to this point (which works well I might add) is 
passing the field to a validator on every KeyUp event, and then splicing the 
incomplete entry onto the front of a "prototype" before running it against a 
regex expression.  By using the boolean states of the regex acting on the 
"spliced" string and the regex of the string itself as two state variables, 
it is easy to (a) find out if the input is correct [so far], (b) find out if 
the whole string is complete and correct, and (c) if it should reject the 
latest input as "wrong", and return to the previous (correct) but partial 
entry.

This method works by using an array of regex expressions paired with their 
complementary prototypes.  Usually a dozen or so "pairs" of this sort will 
handle a fairly complicated input regime, and it can use a common back-end 
syntax checker.

The problem arises when you're dealing with situations in which an enormous 
number of regex "or's" come into play -- each permutation requires a 
separate prototype.  For example, the classic validation of an IP address 
requires 81 prototype/regex pairs -- and it isn't even the hairiest problem 
of this type I've had to tackle!  [3 x 3 x 3 x 3 = 81]

====================================================================================

My question is fairly simple: does Javascript have the ability to test a 
string against a regex expression, and generate a boolean response for 
PARTIAL matches, up to the length of the as-yet-incomplete string?  I've 
poured through the literature, and none of the search/test/exec/RegExp 
discussions have touched on anything other than a match against a complete 
pattern.  I am conversant with the issues of greedy/lazy regular 
expressions, but this seems only to influence the scope of the checking --  
not the issue of total/partial pattern matching. If anybody has cracked this 
particular puzzle, I'd welcome some direction.

-- Dave Lovering 





More information about the Javascript mailing list