View Full Version : Forcing only numbers with .0 or .5 only

01-27-2005, 04:17 AM
Dear all, is there any reference to this javascript that forces a user to enter a number that is either a whole number or only with a .5 decimal? e.g. 1, 2, 3.5, 8.5, 9.0 etc. If the user enters say 8.4, 9.3, 7.7, an error message will appear and clears the current textbox?

Philip M
01-27-2005, 08:49 AM
if (/\.[12346789]/g.test(thetextbox.value)) {
alert ("Error");
else {

...... OK

European users my place a comma , as seperator so better

if (/(\.|\,)[12346789]/g.test(thetextbox.value)) {

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

Philip M
01-28-2005, 08:09 AM
In fact on second thoughts this has an imperfection. It will block 6.8 for example but not 6.08.

Another approach is simply using modulo arithmetic:

x = thetextbox.value
if ((x%0.5) !=0 ) {
alert ("Error!");

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum