jQuery插件Validation表單驗證的示例分析

這篇文章主要介紹jQuery插件Validation表單驗證的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元河池做網(wǎng)站,已為上家服務(wù),為河池各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

具體內(nèi)容如下

功能

1.對”姓名“的必填和長度至少是兩位的驗證。
2.對“電子郵件”的必填和是否為E-mail格式的驗證。
3.對“網(wǎng)址”是否為url的驗證。
4.對”你的評論“是否必填驗證。

結(jié)果圖

jQuery插件Validation表單驗證的示例分析

例子

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>快速上手</title> 
<script type="text/javascript" src="../jquery-1.8/jquery-1.8.0.js"></script> 
<script type="text/javascript" src="../jquery.validate.js"></script> 
<style type="text/css"> 
 *{font-family:Verdana; font-size:96%;} 
 label{width:10em; float:left} 
 label.error{float:none; color:red; padding-left:.5em; vertical-align:top;} 
 p{clear:both} 
 .submit{margin-left:12em;} 
 em{font-weight:bold; padding-right:lem; vertical-align:top;} 
</style> 
<script type="text/javascript"> 
 $(document).ready(function() { 
  $("#commentForm").validate(); 
 }); 
</script> 
</head> 
 
<body> 
 <form class="cmxform" id="commentForm" method="get" action="#"> 
  <fieldset><!--fieldset 元素可將表單內(nèi)的相關(guān)元素分組--> 
   <legend>一個簡單的驗證帶驗證提示的評論例子</legend><!--legend 元素為 fieldset 元素定義標(biāo)題--> 
   <p> 
    <label for="cusername">姓名</label><em>*</em> 
    <input id="cusername" name="username" size="25" class="required" minlength="2"/> 
   </p> 
   <p> 
    <label for="cemail">電子郵件</label><em>*</em> 
    <input id="cemail" name="email" size="25" class="required email"/> 
   </p> 
   <p> 
    <label for="curl">網(wǎng)址</label><em> </em> 
    <input id="curl" name="url" size="25" class="url" value=""/> 
   </p> 
   <p> 
    <label for="ccomment">你的評論</label><em>*</em> 
    <textarea id="ccomment" name="comment" cols="22" class="required"></textarea> 
   </p> 
   <p> 
    <input class="submit" type="submit" value="提交"/> 
   </p> 
  </fieldset> 
 </form> 
</body> 
</html>

代碼分析

1.jQuery代碼

<script type="text/javascript" src="../jquery-1.8/jquery-1.8.0.js"></script> 
<script type="text/javascript" src="../jquery.validate.js"></script> 
<script type="text/javascript"> 
 $(document).ready(function() { 
  $("#commentForm").validate(); 
 }); 
</script>

第1、2行是導(dǎo)入jQuery類庫和Validation插件。
第4行的作用是當(dāng)代碼加載完后,執(zhí)行function中的代碼
第5行是確定哪個表單需要被驗證。

2.針對不同的字段,進行驗證規(guī)則編碼,設(shè)置字段相應(yīng)屬性
class="required" 為必填
class="required email"為必填而且符合為E-mail格式。
class="url" 為url格式驗證
minlength="2"為最小長度是2

以上是“jQuery插件Validation表單驗證的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享標(biāo)題:jQuery插件Validation表單驗證的示例分析
網(wǎng)站路徑:http://muchs.cn/article30/ppposo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、外貿(mào)建站、小程序開發(fā)網(wǎng)站設(shè)計、移動網(wǎng)站建設(shè)企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)