/    Sign up×
Community /Pin to ProfileBookmark

Validating a text box to be a number divisible by 4

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:

[code=php]
<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>
[/code]

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CharlesOct 09.2003 — [font=monospace]<!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>[/font]
Copy linkTweet thisAlerts:
@TomDenverauthorOct 09.2003 — Thanks for the fast reply, I'll give it a shot in a bit.
Copy linkTweet thisAlerts:
@TomDenverauthorOct 09.2003 — Thanks again Charles, it works ?.
×

Success!

Help @TomDenver spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...