jQuery表單驗(yàn)證功能實(shí)例
本文實(shí)例講述了jQuery表單驗(yàn)證功能。分享給大家供大家參考。具體如下:
這里使用jquery實(shí)現(xiàn)的表單驗(yàn)證效果,以Ajax方式驗(yàn)證你的表單是否填寫正確,如果驗(yàn)證不通過,會(huì)將表單元素背景變成紅色,并給出提示信息,簡(jiǎn)單實(shí)用,jquery表單驗(yàn)證功能已經(jīng)有很多了,本款表單驗(yàn)證特效看上去更簡(jiǎn)單,不懂Ajax的朋友,或許直接套用即可實(shí)現(xiàn)無刷新表單驗(yàn)證功能。
運(yùn)行效果截圖如下:

在線演示地址如下:
http://demo.jb51.net/js/2015/jquery-table-form-check-codes/
具體代碼如下:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery表單驗(yàn)證</title>
<style type="text/css">
body, input, textarea {
font-size:12px;
line-height:18px;
font-family:Verdana, Geneva, sans-serif;
}
input {width:200px;}
.submit {width:120px;}
#error {
color:red;
font-size:10px;
display:none;
}
.needsfilled {
background:red;
color:white;
}
</style>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// Place ID's of all required fields here.
required = ["name", "email", "message"];
// If using an ID other than #email or #error then replace it here
email = $("#email");
errornotice = $("#error");
// The text to show up within a field when it is incorrect
emptyerror = "Please fill out this field.";
emailerror = "Please enter a valid e-mail.";
$("#theform").submit(function(){
//Validate required fields
for (i=0;i<required.length;i++) {
var input = $('#'+required[i]);
if ((input.val() == "") || (input.val() == emptyerror)) {
input.addClass("needsfilled");
input.val(emptyerror);
errornotice.fadeIn(750);
} else {
input.removeClass("needsfilled");
}
}
// Validate the e-mail.
if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
email.addClass("needsfilled");
email.val(emailerror);
}
//if any inputs on the page have the class 'needsfilled' the form will not submit
if ($(":input").hasClass("needsfilled")) {
return false;
} else {
errornotice.hide();
return true;
}
});
// Clears any fields in the form when the user clicks on them
$(":input").focus(function(){
if ($(this).hasClass("needsfilled") ) {
$(this).val("");
$(this).removeClass("needsfilled");
}
});
});
</script>
</head>
<body>
<form action="mail.php" id="theform" name="theform" method="post">
<p><label for="name">Name</label><br /><input id="name" type="text" value="" name="name" /></p>
<p><label for="email">E-mail</label><br /><input id="email" type="text" value="" name="email" /></p>
<p><label for="message">Message</label><br /><textarea id="message" rows="7" cols="30" name="message"></textarea></p>
<p><input class="submit" type="submit" name="submit" value="Submit Form" /></p>
<p id="error">表單中有錯(cuò)誤信息!</p>
</form>
</body>
</html>
希望本文所述對(duì)大家的jquery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jquery左右滾動(dòng)焦點(diǎn)圖banner圖片鼠標(biāo)經(jīng)過顯示上下頁(yè)按鈕
jquery左右滾動(dòng)焦點(diǎn)圖banner圖片,鼠標(biāo)經(jīng)過顯示上下頁(yè),適合寬和高都比較大的頁(yè)面使用附演示,感興趣的朋友可以參考下2013-10-10
jQuery實(shí)現(xiàn)二級(jí)下拉菜單效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)二級(jí)下拉菜單效果的相關(guān)資料,需要的朋友可以參考下2016-01-01
jQuery實(shí)現(xiàn)從身份證號(hào)中獲取出生日期和性別的方法分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)從身份證號(hào)中獲取出生日期和性別的方法,結(jié)合實(shí)例形式分析了jQuery數(shù)學(xué)運(yùn)算與字符串操作相關(guān)技巧,需要的朋友可以參考下2016-02-02
學(xué)習(xí)使用jQuery表單驗(yàn)證插件和日歷插件
這篇文章主要為大家詳細(xì)介紹了jQuery表單驗(yàn)證插件與日歷插件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
jQuery實(shí)現(xiàn)鼠標(biāo)跟隨效果
本文主要分享了jQuery實(shí)現(xiàn)鼠標(biāo)跟隨效果的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
jQuery/JS監(jiān)聽input輸入框值變化實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于jQuery/JS監(jiān)聽input輸入框值變化實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10
Jquery下的26個(gè)實(shí)用小技巧(jQuery tips, tricks & solutions)
前段時(shí)間發(fā)布了Jquery類庫(kù)1.4版本,使用者也越來越多,為了方便大家對(duì)Jquery的使用,下面列出了一些Jquery使用技巧。2010-03-03

