JS验证输入框 输入空格时 提示不能为空。但是当输入框中什么也不输入时,不提示为空。

因为此输入框的值是可以为空的,但当你输入空格或其他的时候就要提示出不能为空。

第1个回答  2011-10-17
if(this.value.length>0 && this.value.replace(/\s+/gi)==''){alert('不能为空');}
第2个回答  推荐于2016-10-17
<form name="free"method="POST"action=""onsubmit="return check()">
<p align="center">
<input type="text"name="t1"size="20">
<input type="submit"value="提交"name="B1">
</p>
</form>

<script LANGUAGE="javascript">
function check()
{
if(document.free.t1.value.length==0){
alert("输入框不能为空!");
document.free.t1.focus();
return false;
}
}
</script>本回答被提问者和网友采纳
相似回答