[英]Validate input field
我正在嘗試找到空的輸入字段!!!
<div style="float: left; margin: 5px;">
<p style="margin-left: 50%;">x</p>
<input id="myInput1" type="number">
</div>
<div style="float: left; margin: 5px;">
<p style="margin-left: 50%;">y</p>
<input id="myInput2" type="number">
</div>
<button style="margin-top: 55px;" onclick="validate1()">some things</button>
<script>
function validate1(){
var one = document.getElementById("myInput1").value;
var two = document.getElementById("myInput2").value;
if(one == null)
alert("missing data in x");
else if (two == null)
alert("missing data in y");
else
alert("yes, it works");
}
</script>
請幫忙!:) 如果輸入字段為空,它不顯示是什么問題??? 我想知道輸入字段是否為空
one == null
告訴您one
是null
還是undefined
,但如果輸入字段為空,則.value
是空字符串,既不是null
也不是undefined
。
相反,你應該使用類似的東西
if(one === '') // compare with empty string directly, I recommend this
if(!one) // check if "falsy" (which for strings mean empty)
if(one.length === 0) // check if string has no length
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.