[Cialug] Javascript help

Kevin C. Smith kevin at linuxsmith.com
Fri Nov 4 17:47:39 CST 2005


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



More information about the Cialug mailing list