溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用jQuery實現表單驗證功能

發布時間:2020-06-13 17:38:41 來源:網絡 閱讀:892 作者:歆冉 欄目:web開發

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
font-family: 微軟雅黑;
}
form span{
color: #f00;
font-weight: bold;
display: none;
}
</style>
<script src="jquery-1.8.3.js"></script>
</head>
<body>
<form>
<p>用戶名:</p>
<p>
<input type="text" name="userName" class="auth">
<span>用戶名不能為空</span>
</p>
<p>密碼:</p>
<p>
<input type="text" name="pwd" class="auth">
<span>長度必須為6位</span>
</p>
<p>確認密碼:</p>
<p>
<input type="text" name="rePwd" class="auth">
<span>兩次密碼必須為一致</span>
</p>
<p>
<button>提交</button>
</p>
</form>
</body>
<script type="text/javascript">

// 用戶名驗證
$('input[name=userName]').blur(function(){
val=$(this).val(); // 獲取用戶名
if(val.length==0){
//a=0;
$(this).data({'num':0}); // 綁定屬性
$(this).next().show(); // 顯示提示消息
}else{
//a=1;
$(this).data({'num':1});
$(this).next().hide(); // 隱藏掉提示信息
}
});
// 密碼驗證
$('input[name=pwd]').blur(function(){

val=this.value;   // 獲取密碼
if(val.length<6){
    //b=0;
    $(this).data({'num':0});   // 綁定屬性
    $(this).next().show();    // 顯示提示信息
}else{
    //b=1;
    $(this).data({'num':1});   // 綁定屬性
    $(this).next().hide();   // 隱藏掉提示信息
}

});

// 確認密碼驗證
$('input[name=rePwd]').blur(function(){
pwd=$('input[name=pwd]').val(); // 獲取密碼
repwd=$(this).val(); // 獲取確認密碼
if(pwd!=repwd){
//c=0;
$(this).data({'num':0}); // 綁定屬性
$(this).next().show(); // 顯示提示消息
}else{
//c=1;
$(this).data({'num':1}); // 綁定屬性
$(this).next().hide(); // 隱藏掉提示信息
}
});

// 表單提交
$('form').submit(function(){
// 讓3個文本框失去焦點
$('input.auth').blur(); // 失去焦點的方法
// r=a+b+c;
// if(r==3){
// return true; // 提交表單
// }else{
// return false; // 阻止表單提交
// }
//
// 第二種方式
r=0;
$('input.auth').each(function(){
r+=$(this).data('num'); // 收集數據
});
if(r==3)
return true; // 提交表單
else
return false; // 不提交表單
});
</script>
</html>

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女