Page 1 of 1

form values with maths

Posted: Mon Aug 09, 2010 4:42 am
by me666
hi all, i have been working on a script for a friend, ran into a bit of a dead end though... the form needs income details, weekly or monthly inputs. we have a field for weekly and a field for monthly, and when somebody puts a weekly, or monthly amount in, the other amount fills it's self in automatically. The only thing is if i enter 10 in weekly i get 43.333333333333336 as monthly. I need this to be rounded or cut to 2 decimal placesand then inserted. Here is the code i have to do it so far

Code: Select all

          <input name="incomemainwk" length="40" id="incomemainwk" value="Main" onFocus="if(this.value=='Main') this.value='';" onblur="if(this.value==''){ this.value='Main'; incomemainmo.value='Main'} else{ incomemainmo.value=(this.value*52)/12};"><br>
		  <input name="incomepartnerwk" length="40" id="incomepartnerwk" value="Partner" onFocus="if(this.value=='Partner') this.value='';" onblur="if(this.value==''){ this.value='Partner'; incomepartnermo.value='Partner'} else{ incomepartnermo.value=(this.value*52)/12};"><br>
		  <input name="incomebenefitswk" length="40" id="incomebenefitswk" value="Benefits" onFocus="if(this.value=='Benefits') this.value='';" onblur="if(this.value==''){ this.value='Benefits'; incomebenefitsmo.value='Benefits'} else{ incomebenefitsmo.value=(this.value*52)/12};"><br>
		  <input name="incomeotherwk" length="40" id="incomeotherwk" value="Other" onFocus="if(this.value=='Other') this.value='';" onblur="if(this.value==''){ this.value='Other'; incomeothermo.value='Other'} else{ incomeothermo.value=(this.value*52)/12};">
<br><br>
          <input name="incomemainmo" length="40" id="incomemainmo" value="Main" onFocus="if(this.value=='Main') this.value='';" onblur="if(this.value=='') this.value='Main';"><br>
		  <input name="incomepartnermo" length="40" id="incomepartnermo" value="Partner" onFocus="if(this.value=='Partner') this.value='';" onblur="if(this.value=='') this.value='Partner';"><br>
		  <input name="incomebenefitsmo" length="40" id="incomebenefitsmo" value="Benefits" onFocus="if(this.value=='Benefits') this.value='';" onblur="if(this.value=='') this.value='Benefits';"><br>
		  <input name="incomeothermo" length="40" id="incomeothermo" value="Other" onFocus="if(this.value=='Other') this.value='';" onblur="if(this.value=='') this.value='Other';">
you may notice i have only done half of the script, this is as if it all needs changing it may be easier not doing the whole script now.
Also how would i make sure the entered value is a number and not letters before it is submitted?
Thanks :D

Re: form values with maths

Posted: Mon Aug 09, 2010 1:52 pm
by PHPHorizons
Hello me666,

The toFixed() method of a Number object will round a number to the nearest decimal. When you are dealing with form inputs, you must convert to number first, then you can use toFixed.

Code: Select all

var formValue = '12.352354'; // let's assume this is a value that came from a form.
formValue = parseFloat(formValue); // we now have a number with a decimal
formValue = formValue.toFixed(2); // we now have '12.35' (this is a string now, as toFixed returns a string)
Does that help?

Re: form values with maths

Posted: Tue Aug 10, 2010 7:32 am
by me666
ah excelent, i actually still had a search across the net yesterday, and also found this on another site... i have already added this, with a bit of tweeking and fidling ive got it doing the job great

thanks for your reply :D