Check if input is number or letter javascript

前端 未结 12 1306
遇见更好的自我
遇见更好的自我 2020-11-29 01:03

I\'m using forms in HTML and javascript. I would like an alert to pop up only if the user inputs a LETTER and clicks submit.

So I have

12条回答
  •  执念已碎
    2020-11-29 01:48

    A better(error-free) code would be like:

    function isReallyNumber(data) {
        return typeof data === 'number' && !isNaN(data);
    }
    

    This will handle empty strings as well. Another reason, isNaN("12") equals to false but "12" is a string and not a number, so it should result to true. Lastly, a bonus link which might interest you.

提交回复
热议问题