如题所述
文本框? <textarea> ?
获取 textarea的值是 .value 。
可以用正则表达式检查是否为空。
正则表达式中 \S 代表不为空的字符,所以得出正则为 /\S/ 。
你可以将 textarea.value 的值与该正则套用,代码是 /\S/.test(textarea.value) 。
它会返回一个布尔值(true/ false)。
你将这个代码套用到实际应用中即可。
不仅是 <textarea>, <input>, string, 你都可以这样去检查。
至于什么时候(点击,键盘,事件等)去检查,你自己用 js 去完成。
获取 textarea的值是 .value 。
可以用正则表达式检查是否为空。
正则表达式中 \S 代表不为空的字符,所以得出正则为 /\S/ 。
你可以将 textarea.value 的值与该正则套用,代码是 /\S/.test(textarea.value) 。
它会返回一个布尔值(true/ false)。
你将这个代码套用到实际应用中即可。
不仅是 <textarea>, <input>, string, 你都可以这样去检查。
至于什么时候(点击,键盘,事件等)去检查,你自己用 js 去完成。
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2016-11-30
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Document</title>
<script type="text/javascript">
function checkText(){
var textValue = document.getElementById("searchbtn").value;
if(textValue == ""){
alert("输入框不能为空!");
}
}
</script>
</head>
<body>
<input id="searchbtn" type="text" onblur="checkText();"></input>
</body>
</html>本回答被提问者和网友采纳
<html xmlns="
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Document</title>
<script type="text/javascript">
function checkText(){
var textValue = document.getElementById("searchbtn").value;
if(textValue == ""){
alert("输入框不能为空!");
}
}
</script>
</head>
<body>
<input id="searchbtn" type="text" onblur="checkText();"></input>
</body>
</html>本回答被提问者和网友采纳
第2个回答 2014-12-10
<input type="text" id="txt" onblur="if(this.value.replace(/^\s*|\s*$/g,'')=='') alert('不能为空');" />