If form element multiple of 12 then...

# Thread: If form element multiple of 12 then...

1. Senior Member
Join Date
Dec 1969
Posts
139

## If form element multiple of 12 then...

I have a form element that is a running total in a shopping cart:<BR><BR>&#060;input type="hidden" name="cartNum" value="&#060;%=cartNum%&#062;"&#062;<BR><BR>When the form is submitted, is there a way to make sure that the value in the field must be a multiple of 12...and if it isn&#039;t, pop up with an error message saying &#039;Order Total must be a multiple of 12&#039;.<BR><BR>Any help greatly appreciated.<BR><BR><BR>cheers<BR>RA<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
206

## RE: If form element multiple of 12 then...

You should use JavaScript on the client side if you need to cheack the form BEFORE it is submitted to the server.<BR><BR>In order to check multiple of 12, your number (cartNum) should divide by 12 without a remainder. There for, you need to use Modulus. Something along the line of:<BR>{if((x%12)!=0){<BR>PopUp your message...<BR>}<BR>(Read about Modulud on http://www.devguru.com/Technologies/ecmascript/quickref/arithmetic_operators.html)

3. Senior Member
Join Date
Dec 1969
Posts
139

## Form calling javascript??

Thanks for that. <BR><BR>I do understand the concept of modulus, however I am just a bit unsure on how to get the form to call a javascript function - test to see whether the value is a multiple of 12, and if it isn&#039;t - popup the error message and stop the form from being processed.<BR><BR>Any help appreciated.<BR><BR><BR>Thanks<BR>RA

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Most common use of JS

Typical usage:<BR><BR>&#060;INPUT Type=Text Name="quantity" <BR>onChange="if ( parseInt(this.value) % 12 != 0 ) alert(&#039;must be multiple of 12&#039;);"&#062;<BR><BR>or<BR><BR>&#060;INPUT Type=Text Name="quantity" onChange="return check(this);"&#062;<BR><BR>&#060;SCRIPT&#062;<BR>f unction check(qtyfld)<BR>{<BR> var qty = parseInt(qtyfld.value);<BR> if ( isNaN(qty) &#124&#124 ( parseInt(qty.value) % 12 != 0 ) )<BR> {<BR> alert("The quantity must be numeric and a multiple of 12");<BR> return false;<BR> }<BR> return true;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>or the more common way:<BR><BR>&#060;FORM ... onSubmit="return validate(this);"&#062;<BR> &#060;INPUT Name="quantity"&#062;<BR> ...<BR>&#060;/FORM&#062;<BR><BR>&#060;SCRIPT&#062;<BR>function validate(frm)<BR>{<BR> if ( ! check(frm.quantity) ) return false; // same function as above<BR><BR> ... other tests ...<BR> <BR> // if all okay<BR> return true;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>If you haven&#039;t started using client-side JS yet, it&#039;s way past time.<BR><BR>A good quicky sort of various tips, etc., is http://www.irt.org and their JS FAQs.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
139

## Thanks Bill

Thanks very much Bill - that&#039;s a great help.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•