Click to See Complete Forum and Search --> : Validating a text box to be a number divisible by 4


TomDenver
10-09-2003, 12:24 PM
My site sells chairs, some of which are only sold in cartons (usually 4 chairs, but sometimes more or less). Currently I have them sold by the carton, so that customers can not try to order just 1 chair. I want to sell them individually, but validate the quantity box to make sure it is a number divisible by 4, but this is way beyond my skills.

Here's the important part of the form, I'm leaving out irrelevant stuff:

<FORM method="post" action="http://www.ewebcart.com/cgi-bin/cart.pl" name=orders >
Quantity:
<INPUT type="text" name="quantity" value="1" maxlength="3" size="1">
<INPUT type="image" src="add.gif" name="add" border="0" class=add
onClick="return validateFurniture();">
</FORM>


As you can see, there is already some validation on this script (checks to see if they clicked a checkbox). That part works fine, so I've not included it in this post. I need some way to check the quantity box on submit, divide it by a number (usually 4, but not always) and see if the result is a whole number. If not, return false and give them an error message.

Charles
10-09-2003, 12:33 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
String.prototype.isFactor = function (n) {return this / n == Math.round(this / n)}
// -->
</script>

<form action="">
<div>
<label>Postal Code<input type="text" onchange="if (!this.value.isFactor(4)) {alert('That number would not appear to be a factor of 4.'); this.value=''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>

TomDenver
10-09-2003, 12:35 PM
Thanks for the fast reply, I'll give it a shot in a bit.

TomDenver
10-09-2003, 12:41 PM
Thanks again Charles, it works :).