js實(shí)現(xiàn)注冊(cè)頁面校驗(yàn)功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)注冊(cè)頁面的校驗(yàn)代碼,供大家參考,具體內(nèi)容如下
基本操作
document.getElementById():獲取頁面元素
alert():向頁面彈出提示框。
innerHTML:操作頁面某個(gè)元素的內(nèi)容,可以獲取,也可以賦值。
document.write():向頁面中寫內(nèi)容。
本案例實(shí)現(xiàn)注冊(cè)表單的基本驗(yàn)證功能,主要實(shí)現(xiàn)非空驗(yàn)證、重復(fù)輸入驗(yàn)證、郵箱驗(yàn)證(正則驗(yàn)證),通過alert提示對(duì)話框給予用戶提示信息。并且當(dāng)用戶輸入非法時(shí)阻止表單提交。
步驟分析:
第一步:綁定事件(onsubmit)。為form表單綁定onsubmit事件,并調(diào)用一個(gè)自定義函數(shù)。
第二步:編寫該函數(shù)(獲取用戶輸入的數(shù)據(jù)<獲取數(shù)據(jù)時(shí)需要在指定位置定義一個(gè) id>)
第三步:對(duì)用戶輸入的數(shù)據(jù)進(jìn)行判斷
第四步:數(shù)據(jù)合法(表單提交)
第五步:數(shù)據(jù)非法(給出錯(cuò)誤提示信息,阻止表單提交)
【問題】如何控制表單提交?
關(guān)于事件 onsubmit:一般用于表單提交的位置,那么需要在定義函數(shù)的時(shí)候給出一個(gè)返回值。 onsubmit = return checkForm()
案例實(shí)現(xiàn)效果:當(dāng)點(diǎn)擊“注冊(cè)”按鈕時(shí),驗(yàn)證表單輸入內(nèi)容是否合法,如果不合法則給出用戶提示對(duì)話框,并且表單無法提交。
<!DOCTYPE html> <html> ?? ?<head> ?? ??? ?<meta charset="utf-8"> ?? ??? ?<title></title> ?? ??? ?<script> ?? ??? ??? ?function checkFrm() { ?? ??? ??? ??? ?var usernameValue = document.getElementById("username").value; ?? ??? ??? ??? ?if(usernameValue == ""){ ?? ??? ??? ??? ??? ?alert("用戶名不能為空"); ?? ??? ??? ??? ??? ?return false; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?var passwrodValue = document.getElementById("password").value; ?? ??? ??? ??? ?if(passwrodValue ==""){ ?? ??? ??? ??? ??? ?alert("密碼不能為空"); ?? ??? ??? ??? ??? ?return false; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?var emailValue = document.getElementById("email").value; ?? ??? ??? ??? ?var rule = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; ?? ??? ??? ??? ?if(rule.test(emailValue)) { ?? ??? ??? ??? ??? ?alert("輸入郵箱格式非法!"); ?? ??? ??? ??? ??? ?return false; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?</script> ?? ??? ?<style> ?? ??? ??? ?*{ ?? ??? ??? ??? ?margin: 0px; ?? ??? ??? ??? ?padding: 0px; ?? ??? ??? ??? ?box-sizing: border-box; ?? ??? ??? ?} ?? ??? ??? ?body{ ?? ??? ??? ??? ?background-color: azure; ?? ??? ??? ?} ?? ??? ??? ?.rg_layout{ ?? ??? ??? ??? ?width:900px; ?? ??? ??? ??? ?height: 500px; ?? ??? ??? ??? ?margin: auto; ?? ??? ??? ??? ?background-color: white; ?? ??? ??? ??? ?border: 8px solid #EEEEEE; ?? ??? ??? ??? ?margin-top:30px; ?? ??? ??? ?} ?? ??? ??? ?.rg_left { ?? ??? ??? ??? ?border: 1px solid red; ?? ??? ??? ??? ?width: 200px; ?? ??? ??? ??? ?padding: 10px; ?? ??? ??? ??? ?float: left; ?? ??? ??? ?} ?? ??? ??? ?.rg_center{ ?? ??? ??? ??? ? ?? ??? ??? ??? ?width:450px; ?? ??? ??? ??? ?float: left; ?? ??? ??? ?} ?? ??? ??? ?.rg_right{ ?? ??? ??? ??? ?border: 1px solid red; ?? ??? ??? ??? ?width: 200px; ?? ??? ??? ??? ?float: right; ?? ??? ??? ?} ?? ??? ??? ?.td_left { ?? ??? ??? ??? ?width:100px; ?? ??? ??? ??? ?text-align: right; ?? ??? ??? ??? ?height: 45px; ?? ??? ??? ?} ?? ??? ??? ?.td_right{ ?? ??? ??? ??? ? ?? ??? ??? ??? ?padding-left: 15px; ?? ??? ??? ?} ?? ??? ??? ?#username,#password,#email,#tel ,#name,#birthday,#checkcode{ ?? ??? ??? ??? ?width: 251px; ?? ??? ??? ??? ?height: 32px; ?? ??? ??? ??? ?border: 1px solid #A6A6; ?? ??? ??? ??? ?border-radius: 5px; ?? ??? ??? ??? ?padding-left: 10px; ?? ??? ??? ?} ?? ??? ??? ?#checkcode{ ?? ??? ??? ??? ?width: 110px; ?? ??? ??? ?} ?? ??? ??? ?#btn-sub{ ?? ??? ??? ??? ?width:150px; ?? ??? ??? ??? ?height:40px; ?? ??? ??? ??? ?background-color: #00CCFF; ?? ??? ??? ??? ?border:1px solid #00CCFF; ?? ??? ??? ? ? ?border-radius: 5px; ?? ??? ??? ?} ?? ??? ??? ?#img_check{ ?? ??? ??? ??? ?height: 32px; ?? ??? ??? ??? ?vertical-align: middle;//垂直居中 ?? ??? ??? ?} ?? ??? ?</style> ?? ?</head> ?? ?<body> ?? ??? ?<div class="rg_layout"> ?? ??? ??? ?<div class="rg_left"> ?? ??? ??? ??? ?<p>新用戶注冊(cè)</p> ?? ??? ??? ??? ?<P>USER REGISTER</P> ?? ??? ??? ?</div> ?? ??? ??? ?<div class="rg_center"> ?? ??? ??? ??? ?<form action="#" method="get" onsubmit="return checkFrm()"> ?? ??? ??? ??? ??? ?<table> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"> ?? ??? ??? ??? ??? ??? ??? ??? ?<label for="username">用戶名:</label> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="text" name="username" placeholder="請(qǐng)輸入用戶名" id="username"> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"> ?? ??? ??? ??? ??? ??? ??? ??? ?<label for="password">密碼:</label> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="password" name="password" placeholder="請(qǐng)輸入密碼"id="password"> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"> ?? ??? ??? ??? ??? ??? ??? ??? ?<label for="email">Email:</label> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="email" name="email" ?id="email"> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"> ?? ??? ??? ??? ??? ??? ??? ??? ?<label for="name">姓名:</label> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="text" name="name" id="name"> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"> ?? ??? ??? ??? ??? ??? ??? ??? ?<label for="tel">手機(jī)號(hào):</label> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="text" name="tel" id="tel"> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"><label >性別:</label></td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="radio" name="gender" value="man">男 ?? ??? ??? ??? ??? ??? ??? ? ? ?<input type="radio" name="gender" value="woman">女 ?? ??? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"><label for="birthday">出生日期</label></td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="date" name="birthday" id="birthday"> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_left"><label for="checkcode">驗(yàn)證碼</label></td> ?? ??? ??? ??? ??? ??? ??? ?<td class="td_right"> ?? ??? ??? ??? ??? ??? ??? ??? ?<input type="text" name="checkcode" id="checkcode"> ?? ??? ??? ??? ??? ??? ??? ??? ?<img src="#" id="img_check"> ?? ??? ??? ??? ??? ??? ??? ?</td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td colspan="2" align="center"><input type="submit" value="注冊(cè)" id="btn-sub"/></td>?? ? ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ?</table>?? ??? ? ?? ??? ??? ??? ?</form>?? ? ?? ??? ??? ?</div> ?? ??? ??? ?<div class="rg_right"> ?? ??? ??? ??? ?<P>已有賬號(hào)<a href="#" rel="external nofollow" >立即登錄</a></P>?? ? ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript正則表達(dá)式實(shí)現(xiàn)注冊(cè)信息校驗(yàn)功能
- javascript使用正則表達(dá)式實(shí)現(xiàn)注冊(cè)登入校驗(yàn)
- js實(shí)現(xiàn)登錄注冊(cè)框手機(jī)號(hào)和驗(yàn)證碼校驗(yàn)(前端部分)
- JavaScript 完成注冊(cè)頁面表單校驗(yàn)的實(shí)例
- JavaScript注冊(cè)時(shí)密碼強(qiáng)度校驗(yàn)代碼
- js簡(jiǎn)單實(shí)現(xiàn)用戶注冊(cè)信息的校驗(yàn)代碼
- 攔截JSP頁面,校驗(yàn)是否已登錄詳解及實(shí)現(xiàn)代碼
- 微信+angularJS的SPA應(yīng)用中用router進(jìn)行頁面跳轉(zhuǎn),jssdk校驗(yàn)失敗問題解決
相關(guān)文章
js調(diào)用AJAX時(shí)Get和post的亂碼解決方法
在使用"get"時(shí),抓取的頁面最后加上編碼類型,在使用post時(shí)用vbscript解決了編碼問題,具體實(shí)現(xiàn)如下,有類似情況的朋友可以參考下哈2013-06-06Js注冊(cè)協(xié)議倒計(jì)時(shí)的小例子
Js注冊(cè)協(xié)議倒計(jì)時(shí)的小例子,需要的朋友可以參考一下2013-06-06使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的哈希映射功能
哈希表大家應(yīng)該都經(jīng)常用到吧,那么大家有沒有想過哈希表是怎么實(shí)現(xiàn)的呢,本文我們就來從一道簡(jiǎn)單的題目來了解一下哈希表的簡(jiǎn)單原理和實(shí)現(xiàn)吧2024-02-02Javascript動(dòng)態(tài)創(chuàng)建表格及刪除行列的方法
這篇文章主要介紹了Javascript動(dòng)態(tài)創(chuàng)建表格及刪除行列的方法,涉及javascript動(dòng)態(tài)操作表格的相關(guān)技巧,需要的朋友可以參考下2015-05-05淺談JavaScript中面向?qū)ο蠹夹g(shù)的模擬
淺談JavaScript中面向?qū)ο蠹夹g(shù)的模擬...2006-09-09基于javascript實(shí)現(xiàn)頁面加載loading效果
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)頁面加載loading效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03