Hi thanks for reading in advance - I am developing a few calculators and want to incorporate some isNaN validation, using return true, return false to prevent the calculator displaying isNaN if users input non numbers into the fields, and to run the calculator function only if all fields are valid number strings. However I am not entirely sure whether to run a seperate function prior to the calculator using return true or to place a if else function within the calculator function and basically could use a hand. Here's the code I was trying which isn't working at the moment; I am using the onclick event handler to return onclick="maxloan(this.form)" I am just trying to validate one field first and then will just extend the function for the rest. Help really appreciated people! Thanks function maxloan(borrowcalc) { function form1_onsubmit() { var returnValue = false; if (isNaN(document.borrowcalc.basic_salary1.value)) { alert("Please only enter numbers into the calculator fields"); document.borrowcalc.basic_salary1.value(); } else { returnValue = true; } return returnValue; } form1_onsubmit(); var a = parseFloat(document.getElementById("basic_salary1").value); var b = parseFloat(document.getElementById("bonus_app1").value); var c = parseFloat(document.getElementById("loans_app1").value); var d = parseFloat(document.getElementById("cards_app1").value); var aa = parseFloat(document.getElementById("basic_salary2").value); var bb = parseFloat(document.getElementById("bonus_app2").value); var cc = parseFloat(document.getElementById("loans_app2").value); var dd = parseFloat(document.getElementById("cards_app2").value); var x = parseFloat(document.getElementById("dependents").value); x=((x*120)*12); xx=((c+cc)*12+(((d+dd)*0.03)*12)) var ii=((a+(b/2))-((c*12)+((d*0.03)*12))); var ff=((aa+(bb/2))-((cc*12)+((dd*0.03)*12))); if (ii>=ff) {var app1=ii-xx; var app2=ff} else {var app1=ff-xx; var app2=ii}; if (ii>=ff) {var app11=ii; var app22=ff} else {var app11=ff; var app22=ii}; if (app11*3.5+app22>=((app11+app22)*2.75)) {var bslow=app11*3.5+app22} else {var bslow=((app11+app22)*2.75)}; if (app11*4+app22>=((app11+app22)*3.75)) {var bsmid=app11*4+app22} else {var bsmid=((app11+app22)*3.75)}; if (app1*4.5+app2>=(((app1+app2)-x)*4.5)) {var bshigh=((app1-x)*4.5+app2)} else {var bshigh=(((app1+app2)-x)*4.5)}; if ((app1+app2)<=30000&&(((app1-x)*5)+app2)>=(((app1+app2)-x)*5)){var bl=((app1-x)*5)+app2} else if ((app1+app2)<=30000&&(((app1-x)*5)+app2)<(((app1+app2)-x)*5)) {var bl=(((app1+app2)-x)*5)} else if ((app1*5.5)+app2>=(((app1-x)+app2)*5.5)) {var bl=((app1-x)*5.5)+app2} else {var bl=(((app1-x)+app2)*5.5)}; var bllow=bl*0.85; var blmid=bl*0.925; bl=bl.toFixed(2); bllow=bllow.toFixed(2); blmid=blmid.toFixed(2); bshigh=bshigh.toFixed(2); bsmid=bsmid.toFixed(2); bslow=bslow.toFixed(2); document.getElementById("high_score").value=bshigh; document.getElementById("medium_score").value=bsmid; document.getElementById("low_score").value=bslow; document.getElementById("high_scoreb").value=bl; document.getElementById("medium_scoreb").value=blmid; document.getElementById("low_scoreb").value=bllow; ;}
Dude!!!!! You know there are calculator libraries already written. Or do you like recreating the wheel? As far as your question is concerned, before running the calculation part do a check to make sure field is not empty and that it only contains valid entry to do calculation. Have that function return true and if so proceed otherwise hit em with a response. -MT
Hi, thanks for the response - I am making calculators that are a little different to those already created so yes re-inventing the wheel is the order of the day. The issue I could do with help on is I cannot get my attempts to validate the fields to work? I have included an example of how I am trying to get this to process. Thanks