完整程序如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function IsNull(){
var str = document.getElementById('str').value.trim();
if(str.length==0){
document.getElementById('1').innerText="对不起,文本框不能为空或者为空格!";
}
}
</script>
</head>
<body>
<form>
<tr>
<td>姓名</td>
<input type="text" id="str" onblur="IsNull()"/>
</tr>
<tr>
<div id="1">
</div>
</tr>
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function IsNull(){
var str = document.getElementById('str').value.trim();
if(str.length==0){
document.getElementById('1').innerText="对不起,文本框不能为空或者为空格!";
}
}
</script>
</head>
<body>
<form>
<tr>
<td>姓名</td>
<input type="text" id="str" onblur="IsNull()"/>
</tr>
<tr>
<div id="1">
</div>
</tr>
</form>
</body>
</html>
var str = trim(document.getElementById('str').value);
if(str.length==0){
alert('对不起,姓名不能为空或者为空格!');
}
}
function trim(str)
{
str = str.replace(/ /g,' ');
for(var i = 0 ; i<str.length && str.charAt(i)==" " ; i++ ) ;
for(var j =str.length; j>0 && str.charAt(j-1)==" " ; j--) ;
if(i>j) return "";
return str.substring(i,j);
}
if(str!="")
var str = document.getElementById('str').value.replace(/\s/g,"");
if(str.length==0){
alert('对不起,姓名不能为空或者为空格!');
}
}