猿问

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

我想检查用户是否在我的输入之一中输入了值。我想检查输入的值既不是空字符串也不是数字。请问我该怎么做?


const tempInput = parseInt(document.querySelector("#temp").value); 

// this is my input


if (tempInput === '' || tempInput !== Nan){

  code block

}

不幸的是,这不起作用!谢谢!!


javascript


HUH函数
浏览 109回答 2
2回答

梦里花落0921

var theInput = document.getElementById("temp");var regex = /(?:[A-Za-z].*?\d|\d.*?[A-Za-z])/;theInput.addEventListener("keyup", function () {&nbsp; &nbsp; if(theInput.value.length == 0){&nbsp; &nbsp; &nbsp; &nbsp;console.log("Is empty");&nbsp; &nbsp; } else if(!!theInput.value.match(regex)){&nbsp; &nbsp; &nbsp; &nbsp; console.log("Both number and characters");&nbsp; &nbsp; } else if(theInput.value.length > 0 && !isNaN(theInput.value)){&nbsp; &nbsp; &nbsp; &nbsp; console.log("Number");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; console.log("NOT a Number");&nbsp; &nbsp; }});<input id="temp" type="text">

白板的微信

var value = document.getElementById("temp").value;if(value.length > 0 && !isNaN(value)){&nbsp; &nbsp; //code}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答