对jqueryValidate的使用心得 这个例子和bootstrap联合用的

首先要知道jqueryValidate一款提供表单验证的插件

        下面是我开发的中的例子
     commentForm 是form的id   , rules是验证规则,messages是返回的错误信息,messages可以省略,单最好根据自己的业务,去定义,如果直接是通过表单提交的,使用下面的方法就行,如果是通过ajax提交的,就要在ajax外面加一层判断
例如
if($("#commentForm").valid()){
         这里写提交的
         var  xxx=("#ddd").val();
         $.ajax({

    });
}

$("#commentForm").validate({
       errorPlacement: function errorPlacement(error, element) {
           if(element.prop("name") == "color"){
              element.parent().parent().before(error);
           }else{
            element.before(error); 
           }
       },
       rules: {
           productBrand: {
               required: true,
           },
           productNameNew: {
               required: true,
           },
           productModel: {
               required: true,
           },
           productBrandWidth: {
               required: true,
           },
           name: {
               required: true,
               maxlength:20
           },
           salePrice: {
               required: true,
               range:[0,9999],
               maxlength:6
           },
           marketPrice:{
               range:[0,9999],
               maxlength:6
           },
           salesPromotion:{
               maxlength:20
           },
           gift:{
               maxlength:85
           }
       },
       messages:{
           productBrand: {
               required: "产品品牌必选",
           },
           productNameNew: {
               required: "产品名称必选",
           },
           productModel: {
               required: "产品型号必选",
           },
           productBrandWidth: {
               required: "带宽必选",
           },
           name: {
               required: "销售品名称不能为空",
               maxlength:"最大长度为20个字符"
           },
           salePrice: {
               required: "销售价不能为空",
               range:"请输入9999以内的数字" ,
               maxlength:"最大长度为6位数字"
           },
           marketPrice:{
               range:"请输入9999以内的数字",
               maxlength:"最大长度为6位数字"
           },
           salesPromotion:{
               maxlength:"最大长度为20个字符"
           },
           gift:{
               maxlength:"最大长度为85个字符"
           }
       }
      });

需要引用的js

<script type="text/javascript"src="jquery-3.1.0.min.js"></script>

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

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

<script type="text/javascript" src="messages_zh.min.js"></script>

jsp
<div class="form-group">
    <label for="name" class="control-label"><labe style="color:red;">*</labe>销售品名称</label>
    <input type="text" name="name" class="form-control" value="" id="name" placeholder="请输入销售品名称">
</div>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值