這篇文章主要介紹“怎么用js實現注冊頁面校驗功能”,在日常操作中,相信很多人在怎么用js實現注冊頁面校驗功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用js實現注冊頁面校驗功能”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
document.getElementById():獲取頁面元素
alert():向頁面彈出提示框。
innerHTML:操作頁面某個元素的內容,可以獲取,也可以賦值。
document.write():向頁面中寫內容。
本案例實現注冊表單的基本驗證功能,主要實現非空驗證、重復輸入驗證、郵箱驗證(正則驗證),通過alert提示對話框給予用戶提示信息。并且當用戶輸入非法時阻止表單提交。
第一步:綁定事件(onsubmit)。為form表單綁定onsubmit事件,并調用一個自定義函數。
第二步:編寫該函數(獲取用戶輸入的數據<獲取數據時需要在指定位置定義一個 id>)
第三步:對用戶輸入的數據進行判斷
第四步:數據合法(表單提交)
第五步:數據非法(給出錯誤提示信息,阻止表單提交)
【問題】如何控制表單提交?
關于事件 onsubmit:一般用于表單提交的位置,那么需要在定義函數的時候給出一個返回值。 onsubmit = return checkForm()
案例實現效果:當點擊“注冊”按鈕時,驗證表單輸入內容是否合法,如果不合法則給出用戶提示對話框,并且表單無法提交。
<!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>新用戶注冊</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="請輸入用戶名" id="username">
</td>
</tr>
<tr>
<td class="td_left">
<label for="password">密碼:</label>
</td>
<td class="td_right">
<input type="password" name="password" placeholder="請輸入密碼"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">手機號:</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">驗證碼</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="注冊" id="btn-sub"/></td>
</tr>
</table>
</form>
</div>
<div class="rg_right">
<P>已有賬號<a href="#" rel="external nofollow" >立即登錄</a></P>
</div>
</div>
</body>
</html>到此,關于“怎么用js實現注冊頁面校驗功能”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。