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 the HTML code:
<form name="myForm" action="" onsubmit="return checkInp()" method="post">
First name: <input type="text" name="age">
<input type="submit" value="Submit">
And the javascript code:
function checkInp()
{
var x=document.forms["myForm"]["age"].value;
if (x consists of any letters) // this is the code I need to change
{
alert("Must input numbers");
return false;
}
}
You could use the isNaN Function. It returns true if the data is not a number. That would be something like that:
Note: isNan considers 10.2 as a valid number.
I think the easiest would be to create a
Number
object with the string and check if with the help ofisInteger
function provided byNumber
class itself.The best and modern way is typeof (variable) if you care about real number not number from string. For example:
you can use isNaN(). it returns true when data is not number.
You can use the isNaN function to determine if a value does not convert to a number. Example as below: