最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript function to validate an integer value - Stack Overflow

programmeradmin0浏览0评论

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))
发布评论

评论列表(0)

  1. 暂无评论