jQuery.validator的非空验证 唯一性验证

Posted 发烧web开发者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery.validator的非空验证 唯一性验证相关的知识,希望对你有一定的参考价值。

<!--editpanl start-->
<div class="modal fade" id="editpanl" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">xxxxxxxxxx</h4>
            </div>
            <form role="form" id="form1"  method="post" class="validate formvalidate">
                <div class="modal-body">
                    <div class="form-group" style="margin: 0 auto;    background-color: #F2F8FE;border: 1px solid #CCE5FF;padding: 0 10px 10px 10px;">
                        <h3 style="color: #4BA9E9;font-weight: bold;font-size: 14px;">温馨提示:</h3>
                        <p style="font-size: 12px;">xxxxxxxxxxxxxxxx!</p>
                    </div>
                    <div class="row" style="margin-top: 20px;">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="inputtel" class="control-label"><span class="required">*</span>xxxxxxxxxx</label>
                                <input type="text" class="form-control" id="inputtel" name="inputtel" maxlength="11"  placeholder=""/>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="inputcode" class="control-label"><span class="required">*</span>xxxxxxx</label>
                                <input  id="inputcode" name="inputcode" type="text" class="form-control"  placeholder=""/>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="inputcarnum" class="control-label"><span class="required">*</span>xxxxxxxxx</label>
                                <input  id="inputcarnum" name="inputcarnum" type="text" class="form-control" placeholder=""/>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="cartype" class="control-label"><span class="required">*</span>xxxxxxxxx</label>
                                <select  class="form-control" id="cartype" name="cartype">
                                    <option value="">--请选择--</option>
                                    <option value="1">xxxxxxxx</option>
                                    <option value="2">xxxxxxxx</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="modal-msg"></div>
                    <button type="submit" class="btn btn-success"  id="btnSave">保存</button>
                    <button type="reset" class="btn btn-white" data-dismiss="modal">关闭</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!--editpanl end-->
/**
 * 非空验证
 * */
htmlObj.formvalidator = function(){
    //表单验证
    $(".formvalidate").validate({
        debug:true,
        submitHandler:function() {
            htmlObj.save();
        },
        onfocusout: function(element) {
            $(element).valid();
        },
        rules: {
            inputtel: {
                required: true,
                isphone:true
            },
            inputcode:{
                required: true,
                maxlength:20,
                uniqueness:true
            },
            cartype: {
                required: true
            },
            inputcarnum: {
                required: true,
                maxlength:20
            }
        }, messages: {
            inputtel: {required:"不能为空!"},
            inputcode:  {
                required:"不能为空!",
                maxlength:"长度不能超过20个字符"},
            cartype:  {required:"不能为空!"},
            inputcarnum:  {
                required:"不能为空!",
                maxlength:"长度不能超过20个字符"}
        }
    });
};
/**
 * 唯一性验证
 * */
jQuery.validator.addMethod("uniqueness",function(){
    var flag=1;
    var resname = $("#resname").val();
    var parentid = $("#parentid").val() || "0";
    var cmd = new Command(‘xxxx‘, ‘xxxx‘, ‘xxxxxxx‘);
    cmd.id = htmlObj.id;
    cmd.rfidcode = $("#inputcode").val();
    cmd.success = function(data) {
        if (data.state == 1){
            if(data.isexist == 1){
                flag=0;
            }
        }
    };
    cmd.execute();
    if (flag == 0)
    {
        return false;
    } else
    {
        return true;
    }
},"已存在!");

 

    jQuery.validator.addMethod("isphone", function(value, element) {
        var length = value.length;
        var regPhone = /^1([3578]\d|4[57])\d{8}$/;
        return this.optional(element) || ( length == 11 && regPhone.test( value ) );
    }, "请正确填写您的手机号码");

 

以上是关于jQuery.validator的非空验证 唯一性验证的主要内容,如果未能解决你的问题,请参考以下文章

Jquery Validator 增加自定义验证方法

jQuery.validator.addMethod自定义验证

jQuery 的 validator 验证动态添加的信息

Java表单中关于JavaScript的非空必输验证,你的程序和大牛有多少差距╮( ̄▽ ̄")╭

jQuery Validator Plugin:验证 div 的内容

使用 Jquery Validator 出现验证错误时如何避免或禁用表单提交