473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Updating Related Text Field using OnChange event

Hi,
I have a requirement in which, I need to capture a loan amount and the
amount of down payment for that loan. According to the requirement,
the user is going to enter enter the loan amount and then will enter
the down payment in either percentage of the loan amount or an actual
amount. I have designed a form to capture this information with three
text fields, one each for loan amount, down payment in % and down
payment in $.
So the user has the option of entering downpayment in $ or in %. If
the user enter the down payment in $ then my code has to calculate the
equivalent % value and populate the corresponding text field and vice
versa. My question here is, what would be the best way to handle this
situation.

Shall i just use the OnChange event of the text field to handle this
scenario? If I do that wont I be running into a loop?

Thanks in Advance
-Santosh
Jul 23 '05 #1
3 7814
"Santosh" <go***********@rediffmail.com> wrote in message
news:1c************************@posting.google.com ...
Hi,
I have a requirement in which, I need to capture a loan amount and the
amount of down payment for that loan. According to the requirement,
the user is going to enter enter the loan amount and then will enter
the down payment in either percentage of the loan amount or an actual
amount. I have designed a form to capture this information with three
text fields, one each for loan amount, down payment in % and down
payment in $.
So the user has the option of entering downpayment in $ or in %. If
the user enter the down payment in $ then my code has to calculate the
equivalent % value and populate the corresponding text field and vice
versa. My question here is, what would be the best way to handle this
situation.

Shall i just use the OnChange event of the text field to handle this
scenario? If I do that wont I be running into a loop?

Thanks in Advance
-Santosh


Will this help? Watch for word-wrap.

<html>
<head>
<title>loan.htm</title>
<script type="text/javascript">
function calc(what) {
var form = document.form1;
var lamt = form.loan_amt.value;
var valu;
if (what == 0) {
valu = lamt / form.down_amt.value;
form.down_pct.value = valu;
} else {
valu = lamt * form.down_pct.value / 100;
form.down_amt.value = valu;
}
}
</script>
</head>
<body>
<p>
<form name="form1">
Loan Amount = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<input type="text" name="loan_amt" size="6" style="text-align:right">
<br>Down Payment Amount = <input type="text" name="down_amt" size="6"
style="text-align:right" onblur="calc(0)">
<br>Down Payment Percent = <input type="text" name="down_pct" size="6"
style="text-align:right" onblur="calc(1)">
<br><input type="button">
</form>
</body>
</html>

There is no data validation not formatting.
Jul 23 '05 #2
Hi McKirahan,
First of all thanks for posting that solution. With a few minor
changes, the code that you posted is working. Here is the final version
of the code..

<html>
<head>
<title>loan.htm</title>
<script type="text/javascript">
function isEmpty(s)
{
return ((s == null) || (s.length == 0) || (Trim(s) == ""));
}
function Trim(s)
{
var xfield = "";
var bTrailing = false;

// first check for leading spaces
for (i = 0; i < s.length; i++)
{
if (bTrailing || (!bTrailing && s.substr(i, 1) != " "))
{
bTrailing = true;
xfield = xfield + s.substr(i, 1);
}
}

// now do trailing
for (i = xfield.length-1; i >= 0; i--)
{
// find last non-space
if (xfield.substr(i, 1) != " ")
{
xfield = xfield.substring(0, i+1);
break;
}
}

return xfield;
}

function calc(entry) {
var form = document.form1;
var lamt = form.loan_amt.value;
var fieldname=entry.name
var fieldvalue=entry.value
//alert(lamt+fieldname+fieldvalue)
var valu;
if(fieldname=="down_amt"){
if(!isEmpty(fieldvalue)){
valu =form.down_amt.value/ lamt *100 ;
form.down_pct.value = valu;
}
}else{
if(!isEmpty(fieldvalue)){
valu = lamt * form.down_pct.value / 100;
form.down_amt.value = valu;
}
}

}
</script>
</head>

<body>
<p>
<form name="form1">

Loan Amount = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<input type="text" name="loan_amt" size="6" style="text-align:right">
<br>Down Payment Amount = <input type="text" name="down_amt" size="6"
style="text-align:right" onblur="calc(this)">
<br>Down Payment Percent = <input type="text" name="down_pct" size="6"
style="text-align:right" onblur="calc(this)">
<br><input type="button">
</form>
</body>
</html>

Jul 23 '05 #3
<go***********@rediffmail.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
Hi McKirahan,
First of all thanks for posting that solution. With a few minor
changes, the code that you posted is working. Here is the final version
of the code..


[snip]

I don't see where you ensure that the data is numeric; look at parseInt()
and parseFloat().

You might want to consider regular expressions for data validation.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike | last post by:
I'm using a script provided by e-mailanywhere, it's a little too big for me. There's 1 text field and 1 password field in a form. OnSubmit, I would like both fields to be validated to look for...
1
by: Roy Adams | last post by:
Hi Group I'm trying to write from dynamically created hidden fields to a txt field in the onchange event handler within a select. basically the number for the for loop is generated by a field...
10
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1)...
1
by: henrylo | last post by:
Hi, I'm using Internet Explorer version 6.0.2900.2180 in my machine. I'm currently have a onchange event on a text element. I want this event to be fired when there is a change in the value...
15
by: webstormcomputers | last post by:
I'm trying to update a hidden field with java script and I'm only getting one of the values from my select stament. Here is the code below. <select name="on0"> <option value="25">25 <option...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
0
by: Mike | last post by:
So here's the situation (.NET 2.0 btw): I have a form, and on this form is a textbox among many other databound controls. The textbox is bound to a field in a data table via the Text property. ...
7
by: kirkgilbert | last post by:
I am trying to do an onchange event in a form using a text field. The form is tied to a record set that is part of a repeated region. One the first record when I edit the data it works perfectly. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.