...

View Full Version : NaN issue



nathanir
09-22-2011, 11:26 AM
I have the following sample html file (attached). I am trying to display the calculated field using javascript and I created a function (I am a newbie) to do so. However, it persistently shows NaN instead of the required number. I have tried my best over a coupla hours racking my brains and the internet as to why it shows as not a number.
I will appreciate any help.
Thanks,

vwphillips
09-22-2011, 11:40 AM
function calculateParaDose () {
var dosage

dosage = (document.Test.paracetamol.value) * (document.Test.Weight.value)
alert("You should give " + dosage + " ml of the paracetamol that you have to your child")
}


or


function calculateParaDose () {
var v1=document.Test.paracetamol.value.replace(/\D/g,'');
document.Test.paracetamol.value=v1;
var v2=document.Test.Weight.value.replace(/\D/g,'');
document.Test.Weight.value=v2;
if (v1&&v2){
alert("You should give " + (v1*v2) + " ml of the paracetamol that you have to your child");
}
else {
alert('only numbers allowed');
}

}

nathanir
09-22-2011, 06:02 PM
Thanks,
That works out well. But there is a fresh problem. It seems to be replacing any decimals and converting say 4.65 to 465 and obviously that gives a totally different result. I tried looking up value.replace(/\D/g,'') on the web but unfortunately do not seem to be getting anywhere. Could you please guide me to a site that sort of explains the parameters that were passed after the value.replace statement?
Rajesh

Philip M
09-23-2011, 08:59 AM
value.replace(/\D/g,'') means replace anything that is not a digit (0-9) with nothing. That includes a decimal point so 4.65 becomes 465. Try this:-



<form name = "Test">
<input type = "text" name = "paracetamol" onkeyup = "checknums(this)">
<input type = "text" name = "Weight" onkeyup = "checknums(this)" >
<input type = "button" value = "Calculate Dose" onclick = "calculateParaDose()">
</form>

<script type = "text/javascript">

function checknums(which) {
var x = which.value;
x = x.replace(/[^0-9\.]/g,""); // only numbers 0-9 and decimal point allowed
which.value = x;
}

function calculateParaDose() {
var v1=Number(document.Test.paracetamol.value);
var v2= Number(document.Test.Weight.value);
if (v1 && v2){
alert("You should give " + (v1*v2) + " ml of the paracetamol that you have to your child");
}
else {
alert ('You must enter a number in both boxes');
return false;
}
}

</script>


"99.9 percent of lawyers give the rest a bad name." - Unknown

nathanir
09-23-2011, 11:25 AM
Thanks again,
It worked well. To try and understand what you are doing, you are first checking whether the input is OK, if not converting it to appropriate characters 0-9 and digit and then doing the calculation.
While I realize that you may not have enough time to answer to this but can you point me to appropriate web resources that will allow me to study this on my own.
Truly appreciate it.
Rajesh

Philip M
09-23-2011, 05:20 PM
I don't really understand what it is precisely that you are wanting, but try Googling for "Javascript regular expressions".

nathanir
09-24-2011, 06:13 PM
That is exactly what I was looking for and found a wealth of material. Now to sift through this. Thanks again!
Rajesh



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum