JavaScript test for integer / string

I couldn’t find either of these functions that satisfied my edge cases or desire for simplicity so I wrote/adapted some of my own. Feel free to use, modify, distribute without attribution, but I’d be eternally grateful if you did ;)

The test for integer is adapted from this post, which is actually almost spot on, apart from their is_int([0]) returns true, as does any integer in a string.

I’m pretty sure I’ve seen this test for string a hundred times on the internet and in books. It is by far the most robust:

Please let me know if you can break it!

359 thoughts on “JavaScript test for integer / string

  1. Hi there! Do you use Twitter? I’d like to follow you if that would be okay. I’m absolutely enjoying your blog and look forward to new posts.

  2. Автор предлагает читателю разные взгляды на проблему, что способствует формированию собственного мнения.

  3. Я оцениваю использование автором разнообразных источников, чтобы подтвердить свои утверждения.

  4. I have learn a few good stuff here. Certainly price bookmarking for revisiting. I surprise how much attempt you put to make this sort of fantastic informative site.

  5. Я впечатлен этой статьей! Она не только информативна, но и вдохновляющая. Мне понравился подход автора к обсуждению темы, и я узнал много нового. Огромное спасибо за такую интересную и полезную статью!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>