JQuery郵箱驗(yàn)證完整代碼
<head>
<title></title>
<style type="text/css">
.txtbackcolor{background-color:#eee;}
</style>
<script src="jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var state = false;
$("#txtEmail").focus(function () {
if (state == false) {
$(this).val('');
}
})
$("#txtEmail").blur(function () {
if ($(this).val() == '') {
$("#spinfo").text("郵箱不能為空");
$(this).focus();
}
else {
if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(this).val()) == false) {
$("#spinfo").text("郵箱格式不正確,請(qǐng)重新填寫");
$(this).focus();
}
else {
$("#spinfo").text('');
$("#spinfo").append("<img src=images/onSuccess.gif />");
state=true;
}
}
})
})
</script>
</head>
<body>
郵箱<input id="txtEmail" type="text" value="輸入郵箱" class="txtbackcolor" /><span id="spinfo"></span><br /></body>
下面就是程序運(yùn)行時(shí)的頁面;
當(dāng)我鼠標(biāo)點(diǎn)擊郵箱的文本框時(shí),文本框里面的字消失;
當(dāng)我什么也不填寫時(shí),就會(huì)出現(xiàn)提示信息;
下面是我隨便寫的字母,不符合郵箱格式,彈出錯(cuò)誤提示;
下面是最后的頁面,當(dāng)我輸入的郵箱的格式正確時(shí),后面的提示信息變成一張圖片。