[Cialug] Javascript help
Jerry Heiselman
jweida at gmail.com
Fri Nov 4 17:53:47 CST 2005
If you are only going to allow two digits before the decimal, then you
could always check to make sure that it isn't longer than 5
characters.
Being that I'm not all that great with regex, I would probably split
the value on the decimal and make sure the left number is a number
and the right number modulus 5 comes out to be zero.
I know that regex is probably more efficient, but when you are
talking about a single entry to check and only one iteration over the
input data, I generally go for simplicity for me rather than
efficiency in the code.
Jerry
On 11/4/05, Kevin C. Smith <kevin at linuxsmith.com> wrote:
> This is by first attempt at this, so be easy on me.
>
> 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:
>
> pattern = /^[0-9]*\.[0][0]{1}$|[2][5]{1}$|[5][0]{1}$|[7][5]{1}$/;
>
> which matches 12.00, 12.25, 12.50, 12.75 (example), but when I attempt to
> also
> match 12.0 or 12.5 I open the door to 12.255555 which I don't want.
>
> I've also noticed the it accecpt stuff like "2,5.25" which I also don't want.
>
> So far I have this:
>
> <script type="text/javascript">
> function checknumber(field,limit)
> {
> pattern = /^[0-9]*\.[0][0]{1}$|[2][5]{1}$|[5][0]{1}$|[7][5]{1}$/;
> if(pattern.test(field.value)==false)
> {
> alert("Please use proper format - 1:15 PM is 13.25");
> field.value = 0;
> }
> else
> {
> if(field.value > limit)
> {
> alert("Out of Range! The range is 0.25-24.0");
> field.value = 0;
> }
> }
> }
> </script>
>
> Any help would be great.
> Thanks,
>
> --
> Kevin C. Smith
>
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>
More information about the Cialug
mailing list