jQuery實(shí)現(xiàn)表單驗(yàn)證功能
jQuery表單驗(yàn)證實(shí)例 / 包含用戶名、密碼、住址、郵箱驗(yàn)證
如下圖

別忘了引入jQuery框架?。。?/p>
話不多說直接先上jQuery部分代碼:
<script type="text/javascript">
$(document).ready(function(){
var tip1 = "<span class='span1'>用戶名不能為空!</span>";//聲明發(fā)生錯誤時在輸入框后面添加的span
var tip2 = "<span class='span2'>郵箱格式錯誤或不能為空!</span>";
var tip3 = "<span class='span3'>地址不能為空!</span>";
var tip4 = "<span class='span4'>密碼長度不能小于五位且最多為十位 !</span>";
var condition = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;//聲明判定郵箱格式的條件
$(".id").blur(function(){
if(!$(".id").val()){//判定用戶名非空
$(".span1").remove();
$(".id").after(tip1);
}
else{
$(".span1").remove();
}
});
$(".email").blur(function(){
if(!condition.test($(".email").val())){//判定郵箱格式
$(".span2").remove();
$(".email").after(tip2);
}
else{
$(".span2").remove();
}
});
$(".adress").blur(function(){
if(!$(".adress").val()){//判定地址非空
$(".span3").remove();
$(".adress").after(tip3);
}
else{
$(".span3").remove();
}
});
$(".pwd").blur(function(){
if($(".pwd").val().length < 5||$(".pwd").val().length >10){//判定密碼長度不能小于5位且不能大于10位
$(".span4").remove();
$(".pwd").after(tip4);
}
else{
$(".span4").remove();
}
});
$(".button").click(function(){//符合所有條件則彈出彈窗表單驗(yàn)證通過,如果不符合則彈出彈窗提醒用戶更改
if(!$(".id").val()||!condition.test($(".email").val())||!$(".adress").val()||$(".pwd").val().length < 5||$(".pwd").val().length >10){
alert("注冊信息有誤,請更改個人信息");
}
else{
alert("注冊成功");
}
})
})
</script>
結(jié)構(gòu)和樣式:
<div class="main_box">
<div class="title">
歡迎注冊原魔
</div>
<div class="box">
<img alt="插圖" src="./img/可莉派萌.png" class="img">
<form>
用戶名:<input class="id" type="text" ><br>
郵 箱:<input class="email" type="text"><br>
地 址:<input class="adress" type="text"><br>
密 碼:<input class="pwd" type="password"><br>
<button type = "button" class="button">注   冊</button>
</form>
</div>
</div>
span{
color:white;
}
body{
font-family: sans-serif;
}
.main_box{
width: 100%;
height: 910px;
background-color: red;
background-image: linear-gradient(#e66465, #000000);
}
.title{
font-size: 5em;
color: white;
width:100%;
height: 100px;
text-align: center;
}
.box{
width: 1050px;
height: 310px;
margin: 150px auto 50px auto;
padding-left: 360px;
}
input{
height: 40px;
width: 200px;
border-radius: 20px;
border: solid 1px #B5B5B5;
margin: 10px;
font-size: 1.2em;
}
form{
color:white;
font-size:1.2em;
float: left;
margin-left: 50px;
}
.button{
width: 280px;
height: 40px;
background-color: #9781FD;
border-radius: 25px;
color:white;
font-size: 1.3em;
font-weight: 700;
margin-top: 10px;
}
.img{
width:310px;
height: 310px;
float: left;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net網(wǎng)站開發(fā)中用jquery實(shí)現(xiàn)滾動瀏覽器滾動條加載數(shù)據(jù)(類似于騰訊微博)
騰訊微博提供兩種加載數(shù)據(jù)的方式,一種是分頁,一種是滾動瀏覽器滾動條加載數(shù)據(jù),分頁功能我想大家都做得太多了,今天我與大家分享一下我用滾動條滾動加載數(shù)據(jù)2012-03-03
今天抽時間給大家整理jquery和ajax的相關(guān)知識
jquery ajax2015-11-11
jquery 點(diǎn)擊元素后,滾動條滾動至該元素位置的方法
下面小編就為大家?guī)硪黄猨query 點(diǎn)擊元素后,滾動條滾動至該元素位置的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
jQuery實(shí)現(xiàn)淡入淡出二級下拉導(dǎo)航菜單的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)淡入淡出二級下拉導(dǎo)航菜單的方法,涉及jquery簡單操作頁面元素展開與隱藏的實(shí)現(xiàn)技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-08-08
jQuery實(shí)現(xiàn)帶進(jìn)度條的輪播圖
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)帶進(jìn)度條的輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09

