checking whether textfield contains letters in javascript

前端 未结 2 1353
我寻月下人不归
我寻月下人不归 2021-01-07 09:50

I am new at Javascript and I wanted to know if there is a way to check if textfield input contains anything other than numbers.

I know how to do that in Java, but Ja

2条回答
  •  孤街浪徒
    2021-01-07 10:15

    You can use isNaN() to check if the input is a number or not.

    HTML:

    
    
    

    JavaScript:

    function checkInput()
    {
      var textCheck = document.getElementById("inputText").value;
      if(isNaN(textCheck))
      {
        document.write("contains letters");
      }
      else
      {
        document.write("only numbers");
      }
    }
    

提交回复
热议问题