/    Sign up×
Community /Pin to ProfileBookmark

subtract two fields on submit and alert if negative

How could I take two form fields and subtract them from each other on submit and then have an alert come up only if the amount is negative?

Example:

67 in field 1 is the users limit. 69 in field 2 is what they have ordered.

When user submits I want it to popup and say something like, “we are sorry your order amount exceeds your limit.”

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisSep 09.2008 — <i>
</i>&lt;script type="text/javascript"&gt;
function subtract(f1, f2) {
if (f1 - f2 &lt; 0)
{alert("we are sorry your order amount exceeds your limit.");
return false;}
return true;
}
&lt;/script&gt;
...
&lt;form onsubmit="return subtract(this.fld1.value, this.fld2.value)"&gt;
&lt;input type="text" name="fld1"&gt;
&lt;input type="text" name="fld2"&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@designopauthorSep 09.2008 — Thank you very much Gil. I highly appreciate your help. Thumbs up my friend. ?
Copy linkTweet thisAlerts:
@designopauthorSep 10.2008 — Is it possible to edit this so that in the alert it says how much you are over your limit?
Copy linkTweet thisAlerts:
@cgishackSep 10.2008 — just store the difference between f1 - f2 into a variable.
<i>
</i>var difference = f1 - f2;
if (difference &lt; 0)
{
alert("we are sorry your order amount exceeds your limit. You entered "+ difference);
return false;
}


Drew
Copy linkTweet thisAlerts:
@designopauthorSep 10.2008 — Thank you. Worked awesome!

One final question. How can I have a text field elsewhere on the page populated on the fly with the + difference? Or would this not be possible?
Copy linkTweet thisAlerts:
@cgishackSep 10.2008 — if your textbox was like so..
[code=html]
<input type="text" id="ERROR">
[/code]


you can populate it like so... (by its ID)

<i>
</i>var difference = f1 - f2;
if (difference &lt; 0)
{
alert("we are sorry your order amount exceeds your limit. You entered "+ difference);
[B]document.getElementById('[COLOR="Red"]ERROR[/COLOR]').value = difference;[/B]
return false;
}
Copy linkTweet thisAlerts:
@designopauthorSep 10.2008 — Thanks. That did not work however. Not sure what I did wrong. What I mean by "on the fly", is that in real time on the page as the math is being done the text field changes.

ie. User enters item. math is done on the fly and this field is updated with leftover balance.
×

Success!

Help @designop 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,
)...