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

来源:互联网 发布:商业组织顶级域名 编辑:程序博客网 时间:2024/06/01 07:45
首先要知道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
原创粉丝点击