检查字符串是否为整数

发布于 2024-10-12 04:55:40 字数 218 浏览 8 评论 0原文

我想验证输入字段。用户应输入长度至少为 10 位数字的电话号码。

所以我需要检查非法字符。最好只是检查输入是否是整数。

我想出了这个,但它不起作用(n 是字符串)。

function isInt(n){
    return typeof n== 'number' && n%1==0;
}

有什么想法吗?

I want to validate a input field. The user should type in a phone number with minimum length of 10 digits.

So I need to check for illegal chars. It would be nice just to check wheather the input is an integer or not.

I came up with this but it does not work (n would be the string).

function isInt(n){
    return typeof n== 'number' && n%1==0;
}

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

败给现实 2024-10-19 04:55:40

您可以进行如下测试:

input.length >= 10 && /^[0-9]+$/.test(input)

如果字符串中包含非数字或字符串长度小于 10 个字符,则测试将失败

You can do a test like this:

input.length >= 10 && /^[0-9]+$/.test(input)

That will fail if there are non-digits in the string or the string is less than 10 chars long

庆幸我还是我 2024-10-19 04:55:40

这应该有效((input - 0) 自动尝试将值转换为数字):

function isInt(input){
   return ((input - 0) == input && input % 1==0);
}

关于此问题已经有一个 SO 问题:在 JavaScript 中验证十进制数字 - IsNumeric()

This should work((input - 0) automatically tries to convert the value to a number):

function isInt(input){
   return ((input - 0) == input && input % 1==0);
}

There is already an SO-question about this issue: Validate decimal numbers in JavaScript - IsNumeric()

川水往事 2024-10-19 04:55:40

对您来说可能有点大材小用,但 Google 不久前发布了一个用于手机验证的 。 Java 和 Javascript 变体可用。

Might be an overkill for you, but Google has not too long ago announced a library for phone validation. Java and Javascript variants are available.

破晓 2024-10-19 04:55:40

验证电话号码比检查输入是否为整数稍微复杂一些。例如,电话号码可以并且确实以零开头,因此从技术上讲它不是整数。用户也可以输入破折号:例如:

00 34 922-123-456

因此,对于验证它,您有几个选项:

  • 使用正则表达式进行验证,看看:

    http://regexlib.com/

    这个网站将有数百个示例

  • 使用循环依次检查每个字符,即字符 int 或破折号

,因为后者取决于用户的一致输入,而你不会得到这个

Validating a phone number is a little more complicated than checking if the input is an integer. As an example phone numbers can and do begin with zeros so it isn't technically and int. Also users may enter dashes: For example:

00 34 922-123-456

So, as for validating it you have a couple of options:

  • Use regex expression to validate, have a look at:

    http://regexlib.com/

    this site will have hundreds of examples

  • Use looping to check each characters in turn, i.e. is character int or dash

I would recommend the former as the latter depends on consistent input from users and you aren't going to get that

摘星┃星的人 2024-10-19 04:55:40

为什么不使用:

return (+val === ~~val && null !== val);

作为函数中的 return ?

这是 javascript 控制台的输出

> +"foobar" === ~~"foobar"
  false
> +1.6 === ~~1.6
  false
> +'-1' === ~~'-1'
  true
> +'-1.56' === ~~'-1.56'
  false
> +1 === ~~1
  true
> +-1 === ~~-1
  true
> +null === ~~null // this is why we need the "&& null !== val" in our return call
  true 

Why not use:

return (+val === ~~val && null !== val);

as a return in your function?

this is the output of the javascript console

> +"foobar" === ~~"foobar"
  false
> +1.6 === ~~1.6
  false
> +'-1' === ~~'-1'
  true
> +'-1.56' === ~~'-1.56'
  false
> +1 === ~~1
  true
> +-1 === ~~-1
  true
> +null === ~~null // this is why we need the "&& null !== val" in our return call
  true 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文