【JS】驗證表單的注冊登錄-方法之一

注冊表單的驗證:

注冊表單的代碼為:

【<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)

登錄的表單驗證與注冊的一樣。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容