Hello!

I have a form for calculating the area in width and length. And the Javascript function to calculate.

<div class='controls'> <input type="text" id='dlin' onkeyup='Rech()' value="1" style="width: 30px;"/> х <input type="text" id='shir' onkeyup='Rech()' value="1" style="width: 30px;"/> м <span style="margin-left:50px;">Площадь потолка:<span id='ploshad'>0</span> м<sup>2</sup></span> </div> <script language="JavaScript"> function Rech() { var ploshad = document.getElementById('ploshad'), dlin = parseFloat(document.getElementById('dlin').value), shir = parseFloat(document.getElementById('shir').value); ploshad.innerHTML = (dlin * shir).toFixed(2);</script> 

Calculations happen without problems, only when entering data on the NUM PAD keyboard in the form of decimal numbers in the Russian layout - instead of a period a comma is inserted and this prevents correct calculation.

Friends, Please tell me the code that will replace the comma with a period and where to embed it, knowledge of Javascript is not enough, please explain in more detail!

Thanks in advance.

2 answers 2

If you don’t bother with the function that will receive an integer value from the input field, then you can write this:

 function Rech() { var ploshad = document.getElementById('ploshad'), dlin = parseFloat(document.getElementById('dlin').value.replace(/,/, '.')), shir = parseFloat(document.getElementById('shir').value .replace(/,/, '.')); ploshad.innerHTML = (dlin * shir).toFixed(2); } 

But in general, it would be better to write a function, by returning the value:

 function getFloat(id){ return parseFloat(document.getElementById(id).value .replace(/,/, '.')); } 

    So try

     var a =(dlin * shir).toFixed(2); var str = new String(a); var st = str.replace(",","."); ploshad.innerHTML = st; 
    • how many useless actions that don't solve the problem. - Yura Ivanov
    • Correct if you know how best! - cheh1
    • @flashback, do you distinguish "input" from "output"? The comma must be replaced in the input lines, not in the output. With the withdrawal, and so will be the point. @ cheh1, I apologize, I thought this was your answer, and this is your question ... you are correctly prompted to use replace. Only not to the conclusion (as in this answer), but to the input. - Yura Ivanov
    • one
      @flashback a.toString (); a + "; @ cheh1 dlin = parseFloat (document.getElementById ('dlin'). value.replace (',', '.')) - zb '21
    • one
      var ploshad = document.getElementById ('ploshad'), dlin = document.getElementById ('dlin'). value.replace (',', '.'), shir = document.getElementById ('shir'). value.replace (',', '.'); ploshad.innerHTML = (dlin * shir) .toFixed (2); - Alex Krass