jQuery.validator插件使用

插件下载地址:https://github.com/jquery-validation/jquery-validation/tree/1.17.0

django引入插件:

    <script src="{% static 'js/validation/jquery.validate.min.js' %}"></script>
    <script src="{% static 'js/validation/messages_zh.min.js' %}"></script>


html代码片段

<form class="form-horizontal" method="post" id="logon" name="logon">
                        <fieldset>
                            <!-- Name input-->
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="name">Name</label>
                                <div class="col-md-6">
                                    <input id="name" name="name" type="text" placeholder="Your name"
                                           class="form-control">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-3 control-label" for="password">password</label>
                                <div class="col-md-6">
                                    <input id="password" name="password" type="password" placeholder="Your password"
                                           class="form-control">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-3 control-label" for="confirm_password">confirm password</label>
                                <div class="col-md-6">
                                    <input id="confirm_password" name="confirm_password" type="password"
                                           placeholder="confirm password"
                                           class="form-control">
                                </div>
                            </div>

                            
                            <!-- Form actions -->
                            <div class="form-group" id="commit">
                                <div class="col-md-12 widget col-md-6">
                                    <input id="logon_button" type="submit" class="btn btn-primary btn-md pull-right"
                                           value="注册">
                                </div>
                                <div class="col-md-12 widget col-md-6">
                                    <input type="reset" class="btn btn-primary btn-md" value="重置">
                                </div>

                            </div>
                        </fieldset>
                    </form>

submitHandler:验证通过后的操作,可以写ajax请求

addMethod:自定义验证方法,可以写正则匹配

validate:用于检查

以下为js示例

$.validator.setDefaults({
    submitHandler: function () {
        var values = {};
        var t = $('form#logon').serializeArray();
        $.each(t, function () {
            values[this.name] = this.value;
        });

        var infoP = $('div#modal-1 p#logOnInfo');
        var infoDiv = $('div#modal-1');

        $.ajax({
            type: "POST",
            url: "logon",
            contentType: 'application/json',
            data: JSON.stringify(values),
            success: function (data) {
                infoP.text(data.message);
                infoDiv.addClass("md-show");

            }
        });
    }
});

jQuery.validator.addMethod("isPassword", function (value, element) {
    var Regx = /^[A-Za-z0-9]*$/;

    return Regx.test(value);
}, "密码仅支持数字和字母");

$(function () {

    $("#logon").validate({
        rules: {
            name: {
                required: true,
                minlength: 6
            },
            password: {
                required: true,
                isPassword: true,
                minlength: 6
            },
            confirm_password: {
                required: true,
                isPassword: true,
                minlength: 6,
                equalTo: "#password"
            }
        },
        messages: {
            name: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 6 characters"
            },
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 6 characters long"
            },
            confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 6 characters long",
                equalTo: "Please enter the same password as above"
            }
        },

        errorElement: "em",
        errorPlacement: function (error, element) {
            // Add the `help-block` class to the error element
            error.addClass("help-block");

            // Add `has-feedback` class to the parent div.form-group
            // in order to add icons to inputs
            element.parents(".col-md-6").addClass("has-feedback");

            if (element.prop("type") === "checkbox") {
                error.insertAfter(element.parent("label"));
            } else {
                error.insertAfter(element);
            }

            // Add the span element, if doesn't exists, and apply the icon classes to it.
            if (!element.next("span")[0]) {
                $("<span class='glyphicon glyphicon-remove form-control-feedback'></span>").insertAfter(element);
            }
        },
        success: function (label, element) {
            // Add the span element, if doesn't exists, and apply the icon classes to it.
            if (!$(element).next("span")[0]) {
                $("<span class='glyphicon glyphicon-ok form-control-feedback'></span>").insertAfter($(element));
            }
        },
        highlight: function (element, errorClass, validClass) {
            $(element).parents(".col-md-6").addClass("has-error").removeClass("has-success");
            $(element).next("span").addClass("glyphicon-remove").removeClass("glyphicon-ok");
        },
        unhighlight: function (element, errorClass, validClass) {
            $(element).parents(".col-md-6").addClass("has-success").removeClass("has-error");
            $(element).next("span").addClass("glyphicon-ok").removeClass("glyphicon-remove");
        }
    });
    $("div#modal-1 button.md-close").click(function () {

        $('div#modal-1').removeClass("md-show");
    })
});


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值