[Cialug] Javascript help

Tad Anhalt tja at netins.net
Fri Nov 4 21:10:53 CST 2005


Kevin C. Smith wrote:
> I'm trying to come up with a javascript function to check an html form
> input field. I've got most of it but I'm have trouble with the number after
> the ".". I'm trying to match number in the form of 12.0, 12.5, 12.00,
> 12.25, 12.50, 12.75 for example. I've got the following:

   This is from memory, so may be subtly or even completely wrong...

   Try something like:
	pattern = /((^[0-2][0-3])|(^[0-1]?[0-9]))\.[0-9]{1,2}$/;

   Should allow 0.0 to 23.99 inclusive.  0.1 to 24.0 would use much the 
same technique.

   Or are you just wanting the specific "xx.0, xx.5, xx.00, etc" as you 
have listed above?  Maybe I'm being dense, but it's hard to tell exactly 
what you have in mind here.

 > [...]Please use proper format - 1:15 PM is 13.25[...]
 > [...]The range is 0.25-24.0[...]

   I don't know what you are attempting to actually do here and maybe it 
makes perfect sense in the context you are using it in, but something 
like this would cause me untold grief in phone calls from confused users 
and exasperated help desk operators.

   This is just my opinion, so take it with a large grain of salt.  If 
you are expecting an end user to enter a time (or anything else, 
really).  Pick a format that will make sense to them and then convert it 
to whatever you need on the back end.

   Twenty-four hour format is probably acceptable (but not ideal), 
hundredths of an hour is probably stretching things a little much 
depending on your audience.  Even with a geek audience, I would expect a 
lot of bad data that looks like hundredths, but were actually entered as 
minutes. Actually, scratch that, I don't think geeks would get it right 
either...

   In other words, if there needs to be a conversion from minutes to 
hundredths of an hour, accept the time in minutes and multiply by 1.6+7, 
use a lookup table or whatever you need to do behind the scenes.  If you 
need it converted to hundredths and only accept to the nearest quarter 
hour, accept the full range and round it according to whatever rules you 
have to abide by.

   Last resort, add (a) dropdown(s) with just the values you will allow. 
  This is last resort because it introduces a potential maintenance 
nightmare that nobody wants to deal with and can also lead to additional 
UI issues that are preferable not to have to deal with.  There are, of 
course, techniques that help avoid these problems, but I'd prefer to 
just avoid the whole thing if possible.  Not a religious issue, just KISS.

   It's a lot easier for you to do it once than expect everyone who uses 
your form to do it again and again.  As a bonus, you'll have less 
problems with users entering syntactically correct, but factually wrong 
data.

   Not to mention that _when_ the rules change, you won't have to 
(re)train everyone to use a new interface, just change some stuff that's 
transparent to them.

   Now, having said all that...  We've all been in the situation where 
this sort of thing is not possible due to circumstances outside of our 
control so please don't take my comments on this as second guessing or 
mean spirited in any way whatsoever.

HTH,
Tad Anhalt


More information about the Cialug mailing list