English 中文(简体)
我如何确认输入投入箱的价值只是数字?
原标题:How do I validate that value entered into an input box is just numbers?

我想知道,确保用户进入投入箱的价值在网页上是数字的最好和最容易的方法是什么? 在通知他们时,不允许他们进入非数字价值。

是否有任何想法?

感谢

最佳回答

如果只允许煽动性价值观,我就试图做这样的尝试:

if( isNaN(parseInt(str, 10)) ) {
    //error
}

EDIT: @M28 and @unomi are right, this might allow some inputs that aren t really numbers. M28 posted a really good solution in the comments:

if( ! (parseInt(str, 10).toString() == str) ) {
    //error
}
问题回答

您可定期使用 expressiond*美元

var re = new RegExp("^d*$");
  if ($("#someinput").val().match(re)) {
    alert("Only numbers");
  } else {
    alert("No match");
  }

^ tells the expression to start matching from the beginning of the string. 
d is a match for numbers which is the same as [0-9] (any number from 0 to 9)
* tells to match all numbers in string.
$ tells the expression to match until the end of the string.

The $("#someinput").val() is jquery but you could use normal javascript as in: document.somepintput.value.match(re).

定期表达是值得学习的,因为它们能够以冷静和简洁的方式解决许多问题。 http://en.wikipedia.org/wiki/Regular_expression”rel=“nofollow noreferer”>Regular expression

虽然Nc3b已经关闭,但使用 par子 确实允许你可能不想这样做。

parseInt("133t",10)

结果:133

做得更好

if(isNaN(Number(str, 10)){
  Error;
}

如果你想确保整个扼杀行动必须代表若干人。

你们可以有一只手脚印,检查可能使用的是NAN功能,看所输入的数值是多少。

(单位:美元)

法典抓住了投入价值,然后从一开始便进行核对,{5}是数位数(这是任择的)。

function validzip(){ txt1 = document.FormName.InputName.value; if (/^d{5}$/ .test(txt1) == false){ /* Simpler/longer alternative to this is: if ((/[0-9]/g .test(txt1) == false) || (txt1.length != 5)){ */ alert( Not Valid ); return false; }

        return true;
    }

迄今为止,你最宝贵的是利用一个验证图书馆和(或)隐蔽图书馆为你们做这项工作的人的工作。 你们已经为你们发明了几只冰箱,只为任何汽车开了火。

这里不把某个具体内容连接起来,因为这不是真的,对“javascript 构成验证图书馆”的搜索就会发现几个例子。

<><>Edit>/strong> 我只看一看一看,我就象“

http://dataalbush.com

只允许输入数字。 使用和完全定制也非常容易,更不用提轻重。

在客户方面,从<代码><input category=" number”>开始; (该编码将作为type=“text”/code>在浏览器上处理,而浏览器尚未支持超文本5。 然后使用javascript(如他人所建议的)给非超5浏览器。

Don tabes-side accreditation in case people have javascript und.

Already some good answers have been posted from nc3b and skyfoot.

但nc3b 回答可能因投入制而失败,例如007,并允许大量投入。

And skyfoot s response might not片面值 均允许空洞str光

www.un.org/Depts/DGACM/index_spanish.htm 为了避免这些陷阱。 i 使用了以下代码。

 $(document).ready(function(){
      $("input").change(function(){
      var enteredValue = $(this).val();
      var re = new RegExp("^[-+]?[0-9]+$");
      if (enteredValue != "" && re.test(enteredValue)) {
        alert("You have entered valid integer");
      } else {
        alert("This is not valid integer");
      }
        //alert("The text has been changed.");
      });
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("input").change(function(){

var enteredValue = $(this).val();
var re = new RegExp("^[-+]?[0-9]+$");
  if (enteredValue != "" && re.test(enteredValue)) {
    alert("You have entered valid integer");
  } else {
    alert("This is not valid integer");
  }
    //alert("The text has been changed.");
  });
});
</script>
</head>
<body>

<input type="text">
<p>Write something in the input field, and then press enter or click outside the field.</p>

</body>
</html>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!