欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實(shí)現(xiàn)注冊(cè)頁面校驗(yàn)功能

 更新時(shí)間:2022年08月25日 11:33:24   作者:欲游山河  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)注冊(cè)頁面校驗(yàn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論