jquery.validate.js 驗證的簡單例子__js

來源:互聯網
上載者:User

作為初學者,網上的各種jquery.validate.js的教程真心看不懂。於是,自己摸索著寫了一個簡單的小例子,僅供初學者參考。本著簡潔易懂的原則,去掉了各種css樣式,js特效。閑言少敘,附上源碼。

 <script src="jquery.js" type="text/javascript"></script> <script src="jquery.validate.js" type="text/javascript"></script><style type="text/css">  .msg_invalid {  background-color: #FFF2F2;  border: 1px solid #FFB6B6;  color: #FF5243;  padding: 6px 10px;}</style><div id="container">    <div class="mainbox setup_box">        <div class="hd">            <h3>發布商品</h3>        </div>        <div class="con">            <div class="form_box">                <form method="post" id="apply_form" action="">                    <input type="hidden" name="goods_id" value="{$goods.goods_id}">                    <ul>                        <li>                            <div class="tit">                                <label for="">商品名稱:</label>                            </div>                            <div class="pt">                                <input class="w400 input_plain" type="text" name="goods_name" id="goods_name" maxlength="20" value=" "/>                                <label for="goods_name" class="error msg_invalid" style='display:none;'></label>                            </div>                        </li>                        <li>                            <div class="tit">                                <label for="">商品價格:</label>                            </div>                            <div class="pt">                                <input class="w120 input_plain"  type="text" name="goods_price" id="goods_price" maxlength="20" value=""/>                                <label for="goods_price" class="error msg_invalid" style='display:none;'></label>                            </div>                        </li>                        <li>                            <div>                              <label>庫存:</label>                            </div>                            <div>                                <input id="storage" name="storage"   class="required">                            </div>                        </li>                    </ul>                    <div class="btn_box">                    <span class="f_btn">                        <input type="submit" class="btn_txt J_submit" value="儲存" />                    </span>                    </div>                </form>            </div>        </div>    </div></div><script type="text/javascript">            $("#apply_form").validate({                rules: {                    goods_name: {                        required:true                    },                    goods_price:{                        required:true,                        number:true                    },                    goods_storage:{                        required:true,                        number:true,                        min:1                    }                },                messages: {                    goods_name: {                        required:'商品名稱不可為空'                    },                    goods_price:{                         required:'商品價格不可為空,且必須是數字',                         number:'商品價格不可為空,且必須是數字',                                            },                    goods_storage:{                        required:'商品庫存不可為空,且必須是數字',                        number:'商品庫存不可為空,且必須是數字',                        min:'商品庫存不能小於1'                    }                }            });    </script>


介紹jquery.validate.js外掛程式的內容忽略,著重講解一下使用方法:

1.引入js

 <script src="jquery.js" type="text/javascript"></script>
 <script src="jquery.validate.js" type="text/javascript"></script>

jquery.validate.js 網上很多都可以下載。

2.使用方式

1)第一種:直接寫在代碼中,如商品名稱和商品價格的方式。

$("#apply_form").validate{}
其中,apply_form是表單的id,rules是驗證規則;
messages是驗證方法。
此外,還使用了提示資訊的樣式。

2)第二種:直接使用class="驗證規則",如商品庫存的方式。
<pre name="code" class="html"> <input id="storage" name="storage"   class="required">
這種方式使用提示的驗證資訊,來源於jquery.validate.js 檔案中的massege資訊。請查看jquery.validate.js 檔案。

至此,驗證規則就可以使用了。如不能滿足要求,可自訂驗證函式。
更深入的教程請查看:http://blog.csdn.net/phiberg/article/details/7344853

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.