Categories
JavaScript Answers

How to check if input is number or letter JavaScript?

Spread the love

Sometimes, we want to check if input is number or letter JavaScript.

In this article, we’ll look at how to check if input is number or letter JavaScript.

How to check if input is number or letter JavaScript?

To check if input is number or letter JavaScript, we can use the isNaN function.

For instance, we write

const checkInput = () => {
  const x = document.forms.myForm.age.value;
  if (isNaN(x)) {
    alert("Must input numbers");
    return false;
  }
};

to define the checkInput function.

In it, we get the value from the input with name age in the form with name myForm with document.forms.myForm.age.value.

We check if the value isn’t a number with isNaN.

If it’s true, then x isn’t a number.

Conclusion

To check if input is number or letter JavaScript, we can use the isNaN function.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.