0

I am looking for a way to calculate two numbers, where each of these two numbers have a string beside it in a textbox like "dolar" or "meter", when I add the string in value="" and calculate I get this message: NaN

here is my code:

function myFunction() {
  var y1 = document.getElementById("text1").value;
  var y2 = document.getElementById("text2").value;
  var x = Number(y1) + Number(y2);
  document.getElementById("demo").value = x;
}
<p>
  Enter first number:
  <input type="text" size="10" id="text1" name="text1" onblur="myFunction()"> Enter second number:
  <input type="text" size="10" id="text2" name="text2" onblur="myFunction()">
</p>
<input type="text" id="demo" name="demo" size="10" value="">

5 Answers 5

2

The Number() constructor will reject an input string that isn't completely a number, and will return NaN in that case.

All is not lost however: the parseFloat() function simply stops parsing when it gets to text that isn't part of a number, so a string like "127.45 pounds" will be parsed as the number 127.45.

Sign up to request clarification or add additional context in comments.

Comments

1

This function will give you what you want from what I understand. Might not be perfect but seems to fit what you're after. Note, most of the html code is the same that you have provided above. This code takes two inputs (e.g., text1= "2 metres" and text2= "3 metres") parses the number part of the strings, adds the numbers, and displays the answer in the demo field (e.g., "5"):

    <html>
    <head>
    <script type='text/javascript'>
    function myFunction() {
        var y1 = parseFloat(document.getElementById("text1").value);
        var y2 = parseFloat(document.getElementById("text2").value);

        if ( !isNaN(y1) && !isNaN(y2) )
        {    

            document.getElementById("demo").value = (y1 + y2);
        }
        else 
        {
            if (document.getElementById("demo").value.length > 0)
                document.getElementById("demo").value = ""  
        }
    }

    </script>
    <body>
    <p>
    Enter first number:
    <input type="text" size="10" id="text1" onblur="myFunction()"> Enter second number:
    <input type="text" size="10" id="text2" onblur="myFunction()">  
    </p>
    <input type="text" id="demo" name="demo" size="10" value="">
    </html>

3 Comments

thanks but this is only hide the NaN message, what I need is to calculate the number even if there is a string in the same field
I'm confused @anatermj, because this function will calculate the numbers in text1 and text2 even if there are non-number characters in the strings. The only time it won't do the calculation is when there are no numbers present, as determined by the parseFloat() and subsequent isNaN() method calls. I've run the function using your html example and that's what it does
can you add the input file code with string in value with the script code
1

anater mj, the onblur method gets executed when a user leaves an input field. What you will get is, when a user leaves one of the input fields and has not yet written any value in the other input field, the method myFunction will get called, but the value of one of the input fields will not be able to be converted to a Number since its value is undefined.

I would suggest either adding a button to execute myFunction or to add a defensive clause to your code to check the value of the input fields first.

Comments

1

I would add that your input number inputs don't have an ID, and you're referencing them with document.getElementById("text1"). Add the referenced IDs to the inputs or use getElementsByName

1 Comment

my bad I fixed it
0

If the field having any non numeric value then we will get NaN. An acronym for “Not a Number.”

If the field holds only integer then use below

var x = parseInt(y1) + parseInt(y2);

If float value then

var x = parseFloat(y1) + parseFloat(y2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.