/    Sign up×
Community /Pin to ProfileBookmark

To check Number difference between 2 input fields

Hi,
I need to write a javascript which will calculate the difference between the 2 rates: Current Rate and the Previous Rate. The difference should not be greater than .20. Say for example if the Previous rate was 4.5 and the Current rate user enters 5.5, It should give a message “Difference greater than 20 bps”, You want to continue.

The rows in the table are randomly generated, some time there will be only one row sometime there will be more than 1
Please see HTML code below

Thanks
Ali

<html>
<head>
<title>Form Validation</title>

</head>

<body>
<script language=”JavaScript” type=”text/javascript”>
function saveRates(){
//Check the rate difference between 2 rates. If the rate difference is greater than .20, confirmation message will be displayed: “Difference greater than 20 bps”
//You want to continue
alert(“Save Rate”)

}
</script>

<BR>
<BR>
<table border=”1″ width=”100%”>
<TR>
<TD>

</TD>
<TD>
Current Rate
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Previous Rate
</TD>

</TR>

<TR>
<TD>
<font face=”Arial” size=”2″>
<small>Month1</small>
</font>
</TD>
<TD>
<font face=”Arial” size=”2″>
<small>
<input name=”CurrentMonth1″ size=”15″ type=”text” value=””/>
</small>
</font>
<font face=”Arial” size=”2″>
<small>
&nbsp;&nbsp;<input name=”PreviousMonth1″ size=”5″ type=”text” value=””/>
</small>
</font>
</TD>
</TR>
<TR>
<TD>
<font face=”Arial” size=”2″>
<small>Month2</small>
</font>
</TD>
<TD>
<font face=”Arial” size=”2″>
<small>
<input name=”CurrentMonth2″ size=”15″ type=”text” value=””/>
</small>
</font>
<font face=”Arial” size=”2″>
<small>
&nbsp;&nbsp;<input name=”PreviousMonth2″ size=”5″ type=”text” value=””/>
</small>
</font>
</TD>
</TR>

</tr>
<input LANGUAGE=”JavaScript” type=”button” value=”Save rates” name=”bSaveChanges” ONCLICK=”saveRates();”>

</table>

</body>
</html>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@mjdamatoNov 30.2007 — First, change your input fields to include an id and to have an onchnage trigger as follows:
&lt;input name="CurrentMonth1" id="CurrentMonth1" size="15" type="text" value="" onchange="checkDiff(this);" /&gt;
&lt;input name="PreviousMonth1" id="PreviousMonth1" size="5" type="text" value="" onchange="checkDiff(this);" /&gt;


Then add this function int he head of the document:
&lt;script type="text/javascript"&gt;

function checkDiff(fieldObj) {

//Ensure the field has a numeric value
if (isNaN(fieldObj.value)) {
alert('You must enter a valid number.');
fieldObj.focus();
fieldObj.select();
return false;
}

//Set the selected month identifier
if (fieldObj.name.substr(0,7)=="Current") {
var monthNum = fieldObj.name.substr(12,1);
} else {
var monthNum = fieldObj.name.substr(13,1);
}

//Create object references for both fields
var currObj = document.getElementById('CurrentMonth'+monthNum);
var prevObj = document.getElementById('PreviousMonth'+monthNum);

//Don't validate if either field is empty.
if (currObj.value=='' || prevObj.value=='') { return false; }

//Get the values of the fields
var currVal = currObj.value;
var prevVal = prevObj.value;

//Calculate the difference
var diff = Math.round(Math.abs(currVal-prevVal)*100,2);

if (diff&gt;20) {
alert('The difference is '+diff+'nnThe difference may not be greater than 20 bps.');
return false;
}

return true;

}

&lt;/script&gt;
Copy linkTweet thisAlerts:
@KorNov 30.2007 — We do not do someone else's homework. Can you show us [I]what[/I] code you have tried? We see no javascript effort...?
Copy linkTweet thisAlerts:
@KorNov 30.2007 — Oh, c'mon, [B]mjdamato[/B], give the man a hook, not the fish! ?
Copy linkTweet thisAlerts:
@mjdamatoNov 30.2007 — Oh, c'mon, [B]mjdamato[/B], give the man a hook, not the fish! ?[/QUOTE]

You're absolutely right. I typically chide people for the same thing. I guess I didn't see it as homework.
Copy linkTweet thisAlerts:
@KorDec 01.2007 — You're absolutely right. I typically chide people for the same thing. I guess I didn't see it as homework.[/QUOTE]
maybe it was not a homework. But even if so, we try to be sure that people have basic knowledge about the language they are talking about, otherwise any example or codes of ours will be absolutely useless for them, and a waste of time for us. If someone tries to do a job in a certain programming language, but he is [I]completely out of[/I] the understanding of it, that means he is a lazy student or, worst, an impostor who told his boss that he is able to do that job... ?
×

Success!

Help @alidar777 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...