Psyche
Psyche

Reputation: 8773

JavaScript function to validate an integer value

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.

Upvotes: 2

Views: 4463

Answers (1)

Vic
Vic

Reputation: 12527

You can always check with parseInt:

if (number == parseInt(number))

Upvotes: 6

Related Questions