为了账号安全,请及时绑定邮箱和手机立即绑定

Javascript 如何检查输入值既不是空也不是数字?

Javascript 如何检查输入值既不是空也不是数字?

HUH函数 2023-11-02 17:07:11
我想检查用户是否在我的输入之一中输入了值。我想检查输入的值既不是空字符串也不是数字。请问我该怎么做?const tempInput = parseInt(document.querySelector("#temp").value); // this is my inputif (tempInput === '' || tempInput !== Nan){  code block}不幸的是,这不起作用!谢谢!!javascript
查看完整描述

2 回答

?
梦里花落0921

TA贡献1772条经验 获得超5个赞

var theInput = document.getElementById("temp");

var regex = /(?:[A-Za-z].*?\d|\d.*?[A-Za-z])/;


theInput.addEventListener("keyup", function () {

    if(theInput.value.length == 0){

       console.log("Is empty");

    } else if(!!theInput.value.match(regex)){

        console.log("Both number and characters");

    } else if(theInput.value.length > 0 && !isNaN(theInput.value)){

        console.log("Number");

    } else {

      console.log("NOT a Number");

    }

});

<input id="temp" type="text">


查看完整回答
反对 回复 2023-11-02
?
白板的微信

TA贡献1883条经验 获得超3个赞

var value = document.getElementById("temp").value;

if(value.length > 0 && !isNaN(value))

{

    //code

}


查看完整回答
反对 回复 2023-11-02
  • 2 回答
  • 0 关注
  • 65 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信