...

View Full Version : newbie! Script not working!



Ameslee
06-03-2006, 09:00 AM
Hey, im new at Javascript and hope someone can help me?

I am trying to make a script to calculate shares and come up with a final value. This program will work out the gross and the net value and display it, to get the net there is a 3% commission of the gross. If the commission works out to be less that $30, it is $30. I am having trouble trying to work out why my code isnt even doing any calculations. Its meant to be calculating up to five different shares, but i was trying to get one working first.

As i said i am new at this, hope someone can help me, here is my code:

<HTML>
<HEAD>
<TITLE> Tracking Shares </TITLE>
<script language = "javascript">
function calculate() {
var name = document.frmshares.name.value
var number = document.frmshares.number.value
var price = document.frmshares.price.value

gross = number * price
document.frmshares.gross.value = gross.toFixed(2)
commission = 3/100 * gross

if (commission < 30.00) {
commission = 30.00
}
else
{
return false;
}
net = gross - commission
window.document.frmshares.net.value = net.toFixed(2)
}
</script>
</HEAD>

<BODY>
<h1>Tracking Shares</h1>
<form name=frmshares action="action marker" onsubmit= "return calculate()"> <p>
Share Name:<input type="text" name="name" size="28"><br>
Number of Shares:<select size="1" name="number">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="300">300</option>
<option value="350">350</option>
<option value="400">400</option>
<option value="450">450</option>
<option value="500">500</option>
<option value="550">550</option>
</select><br>
Current Price:<input type="text" name="price"><br>
<input type="submit" value="Calculate Tax" name = "btnSignUp" onsubmit = "calculate()"><br><br>
Gross:<input type="text" name="gross"><br>
Net:<input type="text" name="price"><br>
</form>

</BODY>
</HTML>

vwphillips
06-03-2006, 11:28 AM
<HTML>
<HEAD>
<TITLE> Tracking Shares </TITLE>
<script language = "javascript">
function calculate() {
var name = document.frmshares.name.value
var number = document.frmshares.number.value||10
var price = document.frmshares.price1.value
if (isNaN(number)||isNaN(price)){
alert('Numbers Only for Number and Price');
return false;
}
var gross = number * price
document.frmshares.gross.value = gross.toFixed(2)
commission = 3/100 * gross
if (commission < 30.00) {
commission = 30.00
}
else {
return false;
}
net = gross - commission
document.frmshares.net.value = net.toFixed(2)
return true;
}
</script>
</HEAD>

<BODY>
<h1>Tracking Shares</h1>
<form name=frmshares action="action marker" onsubmit= "return calculate()"> <p>
Share Name:<input type="text" name="name" size="28"><br>
Number of Shares:<select size="1" name="number">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="300">300</option>
<option value="350">350</option>
<option value="400">400</option>
<option value="450">450</option>
<option value="500">500</option>
<option value="550">550</option>
</select><br>
Current Price:<input type="text" name="price1"><br>
<input type="submit" value="Calculate Tax" name = "btnSignUp" onsubmit = "return calculate()"><br><br>
<input type="button" value="Calculate Tax" name = "" onclick = "return calculate()"><br><br>
Gross:<input type="text" name="gross"><br>
Net:<input type="text" name="net"><br>
</form>
</form>

</BODY>
</HTML>

no duplicate variable and NAME name
+ check your names.

best to use the code tags when posting, theres a button for them.

and welcome

Ameslee
06-04-2006, 04:10 AM
Ok this script works, until i add 4 more name, number and price fields. How do i make it work?

boxxer03
06-04-2006, 09:02 AM
thats because you can't have all the fields named the same. you can't have 4 text inputs that are all named "price" you need to name them like "price1", "price2", "price3", etc... so your specifying exactly which "price" field to pull the value from.

vwphillips
06-04-2006, 09:29 AM
Just had a pm to look at this post

cant say much more than boxxer03 posts wihout more information

also pms should not be to make me look, Its up to me if I wish to follow up a post unless you are a paying client

boxxer03
06-05-2006, 12:30 AM
most people on here recieve e-mails when a new message is posted on a thread they've already posted on, basically telling them to check out the new message. anyways, if you want help to get the code working, you should probably post the new, full code your using now, on here.

Ameslee
06-05-2006, 12:33 AM
<HTML>
<HEAD>
<TITLE> Tracking Shares </TITLE>
<script language = "javascript">
function calculate() {
var name = document.frmshares.name.value
var number = document.frmshares.number.value
var price = document.frmshares.price1.value
if (document.frmshares.name.value = "") {
alert("Please enter a share name.");
document.frmshares.name.select();
}
if (document.frmshares.number.value = "") {
alert("Please enter a number of shares.");
document.frmshares.number.select();
}
if (document.frmshares.price1.value = "") {
alert("Please enter a price.");
document.frmshares.price1.select();
}
if (isNaN(number)||isNaN(price)){
alert('Only numeric values in number of shares and the price field');
return false;
}
var gross = number * price
document.frmshares.gross.value = gross.toFixed(2)
commission = 3/100 * gross
if (commission < 30.00) {
commission = 30.00
}
else {
return false;
}
net = gross - commission
document.frmshares.net.value = net.toFixed(2)
return true;
}
</script>
</HEAD>

<BODY>
<h1>Tracking Shares</h1>
<form name="frmshares" action="action marker" onsubmit= "return calculate()"> <p>
Share Name:<input type="text" name="name" size="28"><br>
Number of Shares:<input type="text" name="number"><br>
Current Price:<input type="text" name="price1"><br><br>


<input type="button" value="Calculate Tax" name = "" onclick = "return calculate()"><br><br>
Gross:<input type="text" name="gross"><br>
Net:<input type="text" name="net"><br>
</form>
</form>

</BODY>
</HTML>

Arty Effem
06-05-2006, 02:27 AM
if (commission < 30.00) {
commission = 30.00
}
else {
return false;
}The code seems to work after a fashion, but could you clarify why you're aborting when commission >=30? Also, what is supposed to happen when the commission exceeds the gross?

Please note that when handling money, work in pennies/cents and then format the result, rather than performing calculations on floating point numbers.

Ameslee
06-05-2006, 02:30 AM
The commission for seling the shareholding is calcualted as 3% of the gross value with a minimum of $30.00 being charged.

If the commission works out to be less than $30 well it must be $30.

Does this make sense?

Arty Effem
06-05-2006, 02:57 AM
The commission for seling the shareholding is calcualted as 3% of the gross value with a minimum of $30.00 being charged.

If the commission works out to be less than $30 well it must be $30.

Does this make sense?Yes I knew that; unfortunately it has no bearing upon what I asked.

(Take #2)
In the segment that I quoted, if the commission is 30 or more, the function stops executing and does not display a result. Why?

If the comission of 30 causes the net to be 0 or negative (I.E. a non-viable sale) what if any action should be taken?

Ameslee
06-05-2006, 03:01 AM
so your saying it should return true. I am a bit confused at the moment. Please take it easy with me i am only new at this and still learning.

Is this the reason why it is not working when i add up 2 five lots of fields?

thanks

Ameslee
06-05-2006, 05:42 AM
thats because you can't have all the fields named the same. you can't have 4 text inputs that are all named "price" you need to name them like "price1", "price2", "price3", etc... so your specifying exactly which "price" field to pull the value from.

Doing it this way would make one huge long piece of code right, coz i would have to duplicate the code i already have 4 more times to suite the names i use. Right?

boxxer03
06-05-2006, 07:01 AM
<html>
</head>
<title> Tracking Shares </title>
<script type="text/javascript">
function calculate(whichSet){
var name=document.getElementById('name'+whichSet).value;
var number=document.getElementById('number'+whichSet).value||10;
var price=document.getElementById('price'+whichSet).value;
if (isNaN(number)||isNaN(price)){
alert('Numbers Only for Number and Price');
return false;
}
var gross=number*price;
document.getElementById('gross'+whichSet).value=gross.toFixed(2);
commission=3/100*gross;
if (commission<30){
commission=30;
}
else {
commission=commission;
}
net=gross-commission;
document.getElementById('net'+whichSet).value=net.toFixed(2);
return true;
}
</script>
</head>

<body>
<h1>Tracking Shares</h1>
<form id="frmshares" action="action marker">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="50%"><p>
Share Name:<input type="text" id="name1" size="28"><br>
Number of Shares:<select size="1" id="number1">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="300">300</option>
<option value="350">350</option>
<option value="400">400</option>
<option value="450">450</option>
<option value="500">500</option>
<option value="550">550</option>
</select><br>
Current Price:<input type="text" id="price1" size="20"><br>
<input type="button" value="Calculate Tax" id="btnSignUp1" onClick="return calculate(1)"><br><br>
Gross:<input type="text" id="gross1" size="20"><br>
Net:<input type="text" id="net1" size="20"><br>
</td>
<td width="50%"><p>
Share Name:<input type="text" id="name2" size="28"><br>
Number of Shares:<select size="1" id="number2">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="300">300</option>
<option value="350">350</option>
<option value="400">400</option>
<option value="450">450</option>
<option value="500">500</option>
<option value="550">550</option>
</select><br>
Current Price:<input type="text" id="price2" size="20"><br>
<input type="button" value="Calculate Tax" id="btnSignUp2" onClick="return calculate(2)"><br><br>
Gross:<input type="text" id="gross2" size="20"><br>
Net:<input type="text" id="net2" size="20"><br>
</td>
</tr>
</table>
</form>
</body>

</html>

this should work fine and it should work with any number of sets of input fields you want to add. i added the table just so you can visually see the seperation of the sets of inputs. anyways, what it does is at the top in red right after the 'function calculate' it says (whichSet) that gets the set number from the onClick function of the Calculate button, so if its the first set it returns 1, second set returns 2, etc... and all the form fields are id-ed to match (note: i changed them from name="" to id=""). so name="gross" is now id="gross1" or id="price1". so on and so forth for each set. just check it out, it should work fine, but ad far as if the calculations are correct, you'd have to check that because i don't know how stock calculations work.

Ameslee
06-05-2006, 07:36 AM
is it possible to have only one net and gross field where the totals for both are displayed? How would i do this?

boxxer03
06-05-2006, 08:17 AM
yeah, you can, but your getting into more coding.

Ameslee
06-05-2006, 08:18 AM
how? plz!

boxxer03
06-05-2006, 08:22 AM
it depends on what you want it to show up in. you can make it show up in a textbox, textarea, a div, a table, anything...

Ameslee
06-05-2006, 08:24 AM
can you please tell me what i would have to do, i think its the best way.

Ameslee
06-05-2006, 08:29 AM
i just want it to show up in the text area - gross and net area

boxxer03
06-05-2006, 08:30 AM
i dunno, i'll have to think about it, its late and i'm tired. basically the problem is trying to have it post it in the same area with out doing one of two things. either you'll double your code, because you'll have to run the calculations for both sets of inputs. or run the code for one set, and post it, but that would be complicated and a royal pain without having it delete the old calculations and stuff. because it would have to just add the new text to the end of what was already there or clear the text box out completely and write in it, which leads back to the doubling your code to fix that problem, problem.

Ameslee
06-05-2006, 08:32 AM
thanks, well let me know if you come up with a solution, and if i find one ill let you know.

vwphillips
06-05-2006, 07:04 PM
<HTML>
<HEAD>
<TITLE> Tracking Shares </TITLE>

<script type="text/javascript">
// Verify and Calculate (05-June-2006)
// by: Vic.Phillips http://www.vicsjavascripts.org.uk


function zxcVerify(){
var zxca,zxc0,zxc1,zxc2,zxc3,zxc4,vzcobj,zxcflds,zxcfld,zxcsum,zxccal,zxcnme,zxcmess,zxcalert;
var zxcargs=zxcVerify.arguments;
var zxcfrm=zxcargs[0].form;
if (!zxcfrm.oop){ zxcfrm.oop=new zxcVerifyOOP(zxcfrm); }
zxcfrm.oop.flds='';
for (zxca=1;zxca<zxcargs.length;zxca++){
zxcobj=zxcargs[zxca];
for (key in zxcobj){

if (key=='prefix'){
zxcflds=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc0=0;zxc0<zxcflds.length;zxc0++){
zxcfrm[zxcflds[zxc0].split('-')[0]].prefix=zxcflds[zxc0].split('-')[1];
}
}
// remove prefix
for (zxc1=0;zxc1<zxcfrm.eles.length;zxc1++){
zxcfld=zxcfrm[zxcfrm.eles[zxc1]];
if (zxcfld){
if (zxcfld.value.length>0&&zxcfld.prefix){
zxcfld.value=zxcfld.value.replace(zxcfld.prefix,'');
}
}
}

if (key=='fixed'){
zxcflds=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc0=0;zxc0<zxcflds.length;zxc0++){
zxcfrm[zxcflds[zxc0].split('-')[0]].fix=zxcflds[zxc0].split('-')[1]*1;
}
}

if (key=='complete'){
zxcflds=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc0=0;zxc0<zxcflds.length;zxc0++){
zxcfld=zxcfrm[zxcflds[zxc0]];
zxcfrm.oop.arys(zxcflds[zxc0]);
if (zxcfld.type=='text'){
if (zxcfld.value.length<1){
zxcfld.ary.push('Complete:');
}
}
if (zxcfld.tagName.toUpperCase=='SELECT'){
if (zxcfld.options[0].value.length<1){
zxcfld.selectedIndex=0;
}
if (zxcfld.selectedIndex<1){
zxcfld.ary.push('Complete');
}
}
}
}

if (key=='digits'){
zxcflds=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc0=0;zxc0<zxcflds.length;zxc0++){
zxcfld=zxcfrm[zxcflds[zxc0]];
zxcfrm.oop.arys(zxcflds[zxc0]);
if (isNaN(zxcfld.value)||zxcfld.value.length<1){
zxcfld.ary.push('Must be Digits:');
}
}
}

if (key=='alpha'){
zxcflds=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc0=0;zxc0<zxcflds.length;zxc0++){
zxcfld=zxcfrm[zxcflds[zxc0]];
zxcfrm.oop.arys(zxcflds[zxc0]);
if (!isNaN(zxcfld.value)){
zxcfld.ary.push('Must Be Alpha:');
}
}
}

if (key=='integer'){
zxcflds=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc0=0;zxc0<zxcflds.length;zxc0++){
zxcfld=zxcfrm[zxcflds[zxc0]];
zxcfrm.oop.arys(zxcflds[zxc0]);
if (zxcfld.value%1!=0){
zxcfld.ary.push('Must be an Integer:')
}
}
}
}

if (key=='calculate'){
zxcflds=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc0=0;zxc0<zxcflds.length;zxc0++){
zxcsum='zxcfrm.'+zxcflds[zxc0].split('=')[0]+'=';
zxccal=zxcflds[zxc0].split('=')[1];
for (zxc1=0;zxc1<zxcfrm.eles.length;zxc1++){
if (zxcfrm.eles[zxc1]!='x'){
zxcnme=zxcfrm.eles[zxc1];
zxccal=zxccal.split(zxcnme);
zxccal=zxccal.join('zxcfrm[\''+zxcnme+'\']');
}
}
zxccal=zxccal.replace(/.value/g,'.value*1');
eval(zxcsum+zxccal);
}
}

// toFixed and add prefix
for (zxc1=0;zxc1<zxcfrm.eles.length;zxc1++){
zxcfld=zxcfrm[zxcfrm.eles[zxc1]];
if (zxcfld){
if (zxcfld.value.length>0){
if (zxcfld.fix){ zxcfld.value=(zxcfld.value*1).toFixed(zxcfld.fix); } // toFixed
if (zxcfld.prefix){ zxcfld.value=zxcfld.prefix+zxcfld.value; } // add prefix
}
}
}

zxcmess=['Please Correct:\n'];
zxcflds=zxcfrm.oop.flds.split(',');
if (key=='alert'){
zxcalert=zxcfrm.oop.fflds(zxcobj[key]);
for (zxc1=0;zxc1<zxcflds.length;zxc1++){
for (zxc2=0;zxc2<zxcalert.length;zxc2++){
if (zxcflds[zxc1]==zxcalert[zxc2]){
zxcfrm[zxcflds[zxc1]].alert=true;
}
}
}
}
for (zxc3=0;zxc3<zxcflds.length-1;zxc3++){
zxcfld=zxcfrm[zxcflds[zxc3]];
if (zxcfld.ary){
for (zxc4=0;zxc4<zxcfld.ary.length;zxc4++){
if (zxcfld.alert){ zxcmess.push(zxcfld.ary[zxc4]+' '+(zxcfld.title||zxcfld.name)); }
}
}
zxcfld.ary=null;
zxcfld.alert=null;
}
if (zxcmess.length>1){
alert(zxcmess.join('\n'));
return false;
}
}
}

function zxcVerifyOOP(zxcfrm){
this.oop=zxcfrm;
zxcfrm.eles=[];
zxcall=zxcfrm.elements;
for (var zxca=0;zxca<zxcall.length;zxca++){
zxcfrm.eles.push(zxcall[zxca].name||'x');
}
}

zxcVerifyOOP.prototype.arys=function(zxcfld){
if (!this.oop[zxcfld].ary){ this.flds+=this.oop[zxcfld].name+','; }
if (!this.oop[zxcfld].ary){ this.oop[zxcfld].ary=[]; }
}

zxcVerifyOOP.prototype.fflds=function(zxck){
return zxck.split(',');
}

var check1={prefix:'net- ,gross- ,price1- ',fixed:'net-2,gross-2',complete:'name,number,price1',digits:'number,price1',alpha:'name',integer:'price1',alert:'name,nu mber,price1'};
var check2={calculate:'gross.value=number.value*price1.value;,hidden.value=3/100*gross.value;,net.value=gross.value-hidden.value;'};


</script>
</HEAD>

<BODY>
<h1>Tracking Shares</h1>
<form name=frmshares action="action marker" onsubmit= "return calculate()"> <p>
<input type="hidden" name="hidden" size="28">
Share Name:<input title="Share Name" type="text" name="name" size="28"><br>
Number of Shares:<select size="1" name="number">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="300">300</option>
<option value="350">350</option>
<option value="400">400</option>
<option value="450">450</option>
<option value="500">500</option>
<option value="550">550</option>
</select><br>
Current Price:<input title="Current Price" type="text" name="price1"><br>
<input type="button" value="Calculate Tax" name = "" onclick = "zxcVerify(this,check1,check2)"><br><br>
Gross:<input type="text" name="gross"><br>
Net:<input type="text" name="net"><br>
</form>





</BODY>
</HTML>

Ameslee
06-06-2006, 09:32 AM
<HTML>
<HEAD>
<TITLE> Tracking Shares </TITLE>
<script language = "javascript">
function calculate() {
var name = document.frmshares.name.value
var number = document.frmshares.number.value
var price = document.frmshares.price1.value

if ((name == "") & (number == "") & (price == "")) {
}
else if (name == "") {
alert("Please enter a share name.");
document.frmshares.name.select();
}

else if (number == "") {
alert("Please enter a number of shares.");
document.frmshares.number.select();
}
else if (isNaN(number) == true){
alert('Only numeric values in number of shares');
return true;
}
else if (price == "") {
alert("Please enter a price.");
document.frmshares.price1.select();
}

else if (isNaN(price) == true){
alert('Only numeric values in price field');
return true;
}
if ((isNaN(price) != true) & (isNaN(number) != true)) {

var gross = number * price
document.frmshares.gross.value = gross.toFixed(2)
var commission = 3/100 * gross
if (commission<30){
commission=30;
}
net = gross - commission

document.frmshares.net.value = net.toFixed(2)

return true;
}
}
</script>
</HEAD>

<BODY>
<h1>Tracking Shares</h1>
<form name="frmshares" action="action marker"> <p>
Share Name:<input type="text" name="name" size="28"><br>
Number of Shares:<input type="text" name="number"><br>
Current Price:<input type="text" name="price1"><br><br>


<input type="button" value="Calculate" name = "" onclick = "return calculate()"><br><br>
Gross:<input type="text" name="gross"><br>
Net:<input type="text" name="net"><br>
</form>
</form>

</BODY>
</HTML>

I have completly changed my code as you can see above. I am going to duplicate this so the user can enter 5 different sets of share information. All is working, but i want it not to do anything if i click on the button. At the moment i click and a net value is entered "-30". How do i stop this? Thanks again.

boxxer03
06-06-2006, 10:02 AM
<HTML>
<HEAD>
<TITLE> Tracking Shares </TITLE>
<script language = "javascript">
function calculate() {
var name=document.frmshares.name.value
var number=document.frmshares.number.value
var price=document.frmshares.price1.value

if (name==""&&number==""&&price==""){
}

else if (name==""){
alert("Please enter a share name.");
document.frmshares.name.focus();
}

else if (number==""||isNaN(number)==true){
alert("Please enter a valid number of shares.");
document.frmshares.number.focus();
}
else if (price==""||isNaN(price)==true){
alert("Please enter a valid price.");
document.frmshares.price1.focus();
}

else {
var gross=number*price
document.frmshares.gross.value=gross.toFixed(2)
var commission=3/100*gross
if (commission<30){
commission=30;
}
net=gross-commission

document.frmshares.net.value=net.toFixed(2)

return true;
}
}
</script>
</HEAD>

<BODY>
<h1>Tracking Shares</h1>
<form name="frmshares" action="action marker"> <p>
Share Name:<input type="text" name="name" size="28"><br>
Number of Shares:<input type="text" name="number" size="20"><br>
Current Price:<input type="text" name="price1" size="20"><br><br>

<input type="button" value="Calculate" name="" onclick="return calculate()"><br><br>
Gross:<input type="text" name="gross" size="20"><br>
Net:<input type="text" name="net" size="20"><br>
</form>

</BODY>
</HTML>

here's the same code with some minor modifications. if you have any questions on why i changed the things i changed, then go ahead and ask me.

Ameslee
06-06-2006, 10:56 AM
[code
else if (number==""||isNaN(number)==true){
alert("Please enter a valid number of shares.");
document.frmshares.number.focus();
}
Just a question about why you put the is numeric and weather there is anything in the input field.

Your way seems so much easier to understand, its set out well, thank you very much, ill check it out and see what its doing now.

Ameslee
06-06-2006, 12:28 PM
another question, once i add more fields, does this script total the gross and the net once the calculate is clicked? or do i have to total it at the end?

boxxer03
06-06-2006, 09:46 PM
Just a question about why you put the is numeric and weather there is anything in the input field.

Your way seems so much easier to understand, its set out well, thank you very much, ill check it out and see what its doing now.

I put it like that just to compact the code, instead of having 8 more 'else if' statements than you need, why not just check to see if the field is blank and if it's numeric in the same time step. either one will trip the alert the two vertical lines are the same thing as saying 'OR' so its basically "if number='' OR isNaN then..(do whatever)".

boxxer03
06-06-2006, 09:48 PM
another question, once i add more fields, does this script total the gross and the net once the calculate is clicked? or do i have to total it at the end?

The way its set right now, once you click calculate it does all its checks to see if everything is cool or not and if it is then it will calculate it for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum