admin管理员组

文章数量:1415684

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))

本文标签: JavaScript function to validate an integer valueStack Overflow