1. 程式人生 > >怎麼控制提交的表單項不為空?

怎麼控制提交的表單項不為空?

做了一個表讓別人填,有姓名,電話什麼的,怎麼控制別人提交的資訊不能是空的呢?就是說如果他不填名字或者別的什麼的就點提交,可以給他返回個對話方塊提示他沒有填入需要的資料,而且也不提交到資料庫中,應該用什麼程式碼來實現呢?
如下的程式碼可以實現這個效果:
HTML code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
<script language="javascript">
function checkspace(checkstr) {
var str = '';
for(i = 0; i < checkstr.length; i++) {
str = str + ' ';
}
return (str == checkstr);
}
function checkuser(){
if(checkspace(document.form1.text.value))
{
alert("your name is null");
document.form1.text.focus();
return false;
}
return true
}
</script>
</head>

<body>
<form name="form1" action="#" method="post" onsubmit="return checkuser()">
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>name: <input type="text" size="20" name="text" /></td>
</tr>

<tr>
<td><input type="submit" name="submit" value="提交" /></td>
</tr>

</table>

。。。。。。。。。。。。。。。。

。。。。。。。。。。。。。。。。。。。。。。。。。

。。。。。。。。。。。。。。。。。。。

更多相關文章: