Paul Coroneos Profile imagePaul Coroneos
Published on

Big trouble with parseInt()

Authors
Github:
PaulACoroneos

The need for input validation is important. In my case I was wanting to validate that a URL parameter was infact a valid integer. I went on MDN and found a handy function called parseInt(). I pushed the feature and it appeared to work just fine.

However my QA engineer pointed out that when she tried to use a URL param of 123wlkdfasdfasdf that infact the number 123 was parsed. I re-read MDN and realized that it turns out parseInt will actually parse an integer happily until it hits something thats not an integer. Then it discards the rest of the input.

The solution I went to instead involved using a regex where I basically looks for characters only between 0 - 9.