Check if input is number or letter javascript

前端 未结 12 1305
遇见更好的自我
遇见更好的自我 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:37

    You could use the isNaN Function. It returns true if the data is not a number. That would be something like that:

    function checkInp()
    {
        var x=document.forms["myForm"]["age"].value;
        if (isNaN(x)) // this is the code I need to change
        {
            alert("Must input numbers");
            return false;
        }
    }
    

    Note: isNan considers 10.2 as a valid number.

提交回复
热议问题