简体   繁体   English

用javascript控制浮点值

[英]controling a float value with javascript

to control my input type and verify that its value is pretty a float in my form i deal with it in javascript like that: 为了控制我的输入类型并验证其值是否在我的表单中是一个浮点数,我使用javascript来处理它,如下所示:

function verifierNombre () {
var champ=document.getElementById("nperformed");
var str = champ.value;
if(str.value==' '){champ.focus();}
if (isNaN(str)) {
alert("Invalid Valid! the field must be a number");
champ.focus();
return false;
}
return true;

}

but it still false because it doesn't accept really floats. 但是它仍然是错误的,因为它不接受真正的浮点数。 so when entering a value like '11,2' the alert is declenched. 因此,当输入“ 11,2”之类的值时,警报会变小。

I want to know how can I control float values with javascript. 我想知道如何使用javascript控制浮点值。 thanks. 谢谢。

I believe the problem may be that it's not considering 11,2 to be a valid float, instead expecting something like 11.2 . 我认为问题可能在于它没有考虑11,2是有效的浮动,而是期望像11.2这样的东西。 Give the 11.2 a try and see if it works. 尝试11.2 ,看看它是否有效。 Should that be the problem, I'm not sure if there's some localization option that can be set, or if you need to manually swap the two before running isNaN . 如果那是问题所在,我不确定是否可以设置一些本地化选项,或者不确定是否需要在运行isNaN之前手动交换两者。

Edit Ah, this webpage confirms it: 编辑 Ah, 此网页确认了这一点:

In many countries, it is common practice to use a decimal comma instead of a decimal point. 在许多国家/地区,通常的做法是使用小数逗号而不是小数点。 Unfortunately, in JavaScript you have to use the decimal point, regardless of which country setting you have in your operating system. 不幸的是,无论您在操作系统中使用哪个国家/地区设置,在JavaScript中都必须使用小数点。 In JavaScript, only digits after a decimal comma will be used for calculations, which will produce incorrect results. 在JavaScript中,仅将小数点逗号后的数字用于计算,这将产生错误的结果。

11,2 isn't a valid float. 11,2不是有效的浮点数。 Javascript like (as far as I know) every programming language uses the . 就我所知,Java语言就像每种编程语言都使用. as decimal sign. 作为十进制符号。 so try 11.2 instead. 因此请尝试11.2

If you want to accept the , you can do it like this: 如果您想接受,可以这样进行:

function verifierNombre() {
  var champ=document.getElementById("nperformed");
  var str = champ.value;
  if(str.value==' ') champ.focus();
  if (isNaN(str.replace(',','.')) {
    alert("Invalid Valid! the field must be a number");
    champ.focus();
    return false;
  }
  return true;
}

function localize_float(champ) {
  champ.value = champ.value.replace(",",".");
}

And in your form you add the onsubmit -attribute like this: 然后在表单中添加onsubmit -attribute,如下所示:

<form name="..." action="..." method="..." onsubmit="localize_float(document.getElementById("nperformed");)">

11,2 is not a valid float because the "decimal point" must be a point (.) and not a comma (,). 11,2不是有效的浮点数,因为“小数点”必须是点(。),而不是逗号(,)。

Replace the comma with a point: 用点替换逗号:

var str = champ.value;
str = str.replace(",", ".");

Also, 也,

if(str.value==' '){champ.focus();}

isn't a very good condition. 条件不是很好。 Use a trim function instead and compare with "". 请改用修剪功能,并与“”进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM