检查输入是数字还是字母javascript

Gre*_*ory 62 html javascript forms input

我在HTML和javascript中使用表单.我希望只有在用户输入LETTER并点击时才会弹出警报submit.

所以我有HTML代码:

<form name="myForm" action="" onsubmit="return checkInp()" method="post">
    First name: <input type="text" name="age">
<input type="submit" value="Submit">   
Run Code Online (Sandbox Code Playgroud)

和javascript代码:

function checkInp()
{
var x=document.forms["myForm"]["age"].value;
if (x consists of any letters) // this is the code I need to change
{
alert("Must input numbers");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)

Bat*_*mer 103

您可以使用isNaN函数来确定值是否未转换为数字.示例如下:

function checkInp()
{
  var x=document.forms["myForm"]["age"].value;
  if (isNaN(x)) 
  {
    alert("Must input numbers");
    return false;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 注意空字符串:`isNaN('')=== false;`#jsrage (16认同)
  • 不要这样做,像'0xFF'和'2e32'这样的字符串将通过此验证. (11认同)
  • 你不能用它来检查数字.如果字母表跟随数字,则失败.试试这个"3a". (3认同)
  • 最疯狂的事实是NAN也是一个数字:( (2认同)

小智 29

使用正则表达式仅匹配字母.如果您需要做一些更复杂的事情,那么了解它也是一件好事,比如确保它是一定数量的数字.

function checkInp()
{
    var x=document.forms["myForm"]["age"].value;
    var regex=/^[a-zA-Z]+$/;
    if (!x.match(regex))
    {
        alert("Must input string");
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

更好的做法是拒绝任何数字:

function checkInp()
{
    var x=document.forms["myForm"]["age"].value;
    var regex=/^[0-9]+$/;
    if (x.match(regex))
    {
        alert("Must input numbers");
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你在第二个代码片段中的'x.match(regex)`之前错过了一个`!`吗? (9认同)
  • 这是最干净的解决方案 (3认同)

mae*_*ael 11

您可以使用isNaN 函数.如果数据不是数字,则返回true.那将是这样的:

function checkInp()
{
    var x=document.forms["myForm"]["age"].value;
    if (isNaN(x)) // this is the code I need to change
    {
        alert("Must input numbers");
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:isNan将10.2视为有效数字.


Ram*_*tra 7

更好的(无错误)代码如下:

function isReallyNumber(data) {
    return typeof data === 'number' && !isNaN(data);
}
Run Code Online (Sandbox Code Playgroud)

这也将处理空字符串。另一个原因,isNaN("12")等于false但是"12"一个字符串而不是数字,所以它应该结果为true。最后,一个您可能感兴趣的奖励链接。


小智 6

只需用1除以x%1即可找到余数。如果余数为0,则表示x是整数。否则,您必须显示消息“必须输入数字”。即使在字符串,十进制数字等情况下也可以使用。

function checkInp()
{
    var x = document.forms["myForm"]["age"].value;
    if ((x%1) != 0) 
    {
        alert("Must input numbers");
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)


str*_*7od 6

我知道这篇文章很旧,但它是我搜索时出现的第一个。我尝试过 @Kim Kling RegExp 但它失败了。同样在找到这个论坛之前,我已经尝试了这里列出的几乎所有其他变体。最后,除了我创建的这个之外,它们都没有工作;它工作正常,而且它是 es6:

    const regex = new RegExp(/[^0-9]/, 'g');
    const val = document.forms["myForm"]["age"].value;

    if (val.match(regex)) {
       alert("Must be a valid number");
    }
   
Run Code Online (Sandbox Code Playgroud)


Dee*_*ore 5

您可以使用isNaN()。如果数据不是数字,则返回true。

var data = 'hello there';
if(isNaN(data)){
  alert("it is not number");
}else {
  alert("its a valid number");
}
Run Code Online (Sandbox Code Playgroud)