JavaScript function to validate an integer value - Stack Overflow

I'm building a shopping cart and I would like to use a JavaScript function to validate user input

I'm building a shopping cart and I would like to use a JavaScript function to validate user input when entering the quantity value in the quantity text input. I would like to allow the entering of integer values only (no floats, no other characters).

I know that I can apply this function using onKeyUp event and also I found isNaN() function, but it returns true even for floats (which is not ok).

Can you guys help me out with this one?

Thanks.

I'm building a shopping cart and I would like to use a JavaScript function to validate user input when entering the quantity value in the quantity text input. I would like to allow the entering of integer values only (no floats, no other characters).

I know that I can apply this function using onKeyUp event and also I found isNaN() function, but it returns true even for floats (which is not ok).

Can you guys help me out with this one?

Thanks.

Share Improve this question asked Jun 6, 2010 at 20:07 PsychePsyche 8,78322 gold badges71 silver badges86 bronze badges 3
  • 1 there is a nice jQuery plugin for this: digitalbush./projects/masked-input-plugin – meo Commented Jun 6, 2010 at 20:11
  • Don't forget to validate on the server side, too. It's very easy to stop JavaScript outputting 'irritating' messages like “Hey you! You're supposed to only input numbers here!”. – Marcel Korpel Commented Jun 6, 2010 at 21:03
  • @meo, pretty nice indeed. Thanks. – Psyche Commented Jun 6, 2010 at 22:28
Add a ment  | 

1 Answer 1

Reset to default 6

You can always check with parseInt:

if (number == parseInt(number))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745196911a4616138.html

相关推荐

  • JavaScript function to validate an integer value - Stack Overflow

    I'm building a shopping cart and I would like to use a JavaScript function to validate user input

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信