注冊表單的驗證:
注冊表單的代碼為:
【<div>
<fieldset>
<legend>用戶登錄</legend>
<form name="RegForm" method="post" action="reg.php" onSubmit="return InputCheck(this)">
<label for="username" class="label">用戶名:</label>
<input id="username" name="username" type="text" class="input" />
<span>(必填,3-15字符長度,支持漢字、字母、數(shù)字及_)</span>
<p/>
<p>
<label for="password" class="label">密 碼:</label>
<input id="password" name="password" type="password" class="input" />
<span>(必填,不得少于6位)</span>
<p/>
<p>
<label for="repass" class="label">重復密碼:</label>
<input id="repass" name="repass" type="password" class="input" />
<p/>
<p>
<label for="email" class="label">電子郵箱:</label>
<input id="email" name="email" type="text" class="input" />
<span>(必填)</span>
<p/>
<p>
<input type="submit" name="submit" value=" ?提交注冊 ?" class="left" />
</p>
</form>
</fieldset>
</div>】
JS做判斷的代碼為:
【?
<script language=JavaScript>
<!--
function InputCheck(RegForm)
{
?if (RegForm.username.value == "")
?{
??alert("用戶名不可為空!");
?RegForm.username.focus();
??return (false);
?}
?if (RegForm.password.value == "")
?{
?alert("必須設(shè)定登錄密碼!");
?RegForm.password.focus();
??return (false);
?}
?if (RegForm.repass.value != RegForm.password.value)
?{
??alert("兩次密碼不一致!");
?RegForm.repass.focus();
??return (false);
?}
?if (RegForm.email.value == "")
?{
?alert("電子郵箱不可為空!");
?RegForm.email.focus();
??return (false);
?}
}
//-->
</script>】
1.javascript中focus()函數(shù)是聚焦函數(shù),RegForm.username.focus()是光標在用戶名的文本框中,所以才可以進行判斷。
2.<fieldset>:在數(shù)據(jù)周圍繪制一個框
<legend>:為fieldset元素定義標題(caption)
登錄的表單驗證與注冊的一樣。