Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: NaN issue

  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    NaN issue

    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,
    Attached Files Attached Files

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    	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

    Code:
    	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');
         }
    
    	}
    Last edited by vwphillips; 09-22-2011 at 10:50 AM.
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    nathanir (09-22-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    That worked but a fresh problem

    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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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:-


    Code:
    <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
    Last edited by Philip M; 09-23-2011 at 08:05 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thanks again

    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    I don't really understand what it is precisely that you are wanting, but try Googling for "Javascript regular expressions".

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That is exactly what I was looking for and found a wealth of material. Now to sift through this. Thanks again!
    Rajesh


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •