Jquery 验证器插件未使用复杂名称进行验证

Jquery validator plugin is not validating using complex names

我有如下表格内容。

<form id="edit-form" action="" method="post">
<div class="row">
    <div class="form-group col-md-2 col-xs-2 col-lg-2">
        <label for="id_first_name">First Name:<span class="help-inline">  *</span></label>
        <input class="form-control input-sm" id="id_customer-first_name" maxlength="150" name="customer-first_name" type="text" value="Neville">
    </div>
    <div class="form-group col-md-2 col-xs-2 col-lg-2">
        <label for="id_middle_name">Middle Name:</label>
        <input class="form-control input-sm" id="id_customer-middle_name" maxlength="150" name="customer-middle_name" type="text">
    </div>
    <div class="form-group col-md-2 col-xs-2 col-lg-2">
        <label for="id_last_name">Last Name:<span class="help-inline">  *</span></label>
        <input class="form-control input-sm" id="id_customer-last_name" maxlength="200" name="customer-last_name" type="text" value="Sarginson">
    </div>
    <div class="form-group col-md-2 col-xs-2 col-lg-2">
        <label for="id_telephone">Telephone:<span class="help-inline">  *</span></label>
        <input class="form-control input-sm" id="id_customer-telephone" maxlength="25" name="customer-telephone" placeholder="(XXX) XXX-XXXX" type="text">
    </div>
    <div class="form-group col-md-2 col-xs-2 col-lg-2">
        <label for="id_mobile">Mobile No:</label>
        <input class="form-control input-sm" id="id_customer-mobile" maxlength="25" name="customer-mobile" type="text">
    </div>
    <div class="form-group col-md-2 col-xs-2 col-lg-2">
        <label for="id_work_phone">Work Phone No:</label>
        <input class="form-control input-sm" id="id_customer-work_phone" maxlength="25" name="customer-work_phone" type="text">
    </div>
</div>
</form>

我使用 jquery 验证插件以下列方式验证表单的输入。

$('#edit-form').validate({
            /* @validation states + elements
                ------------------------------------------- */

                errorClass: "state-error",
                validClass: "state-success",
                errorElement: "em",

                /* @validation rules
                ------------------------------------------ */

                rules: {
                        "customer-first_name": {
                                required: true
                        },
                        "customer-last_name": {
                                required: true
                        },
                        "customer-email": {
                                required: true,
                                email: true
                        },
                        "customer-telephone": {
                                required: true
                        }
                },

                /* @validation error messages
                ---------------------------------------------- */

                messages:{
                        "customer-first_name": {
                                required: 'Enter first name'
                        },
                        "customer-last_name": {
                                required: 'Enter last name'
                        },
                        "customer-email": {
                                required: 'Enter email address',
                                email: 'Enter a VALID email address'
                        },
                        "customer-telephone": {
                                required: 'Enter patient\'s telephone number'

                        }
                },

                /* @validation highlighting + error placement
                ---------------------------------------------------- */

                highlight: function(element, errorClass, validClass) {
                        $(element).closest('.field').addClass(errorClass).removeClass(validClass);
                },
                unhighlight: function(element, errorClass, validClass) {
                        $(element).closest('.field').removeClass(errorClass).addClass(validClass);
                },
                errorPlacement: function(error, element) {
                   if (element.is(":radio") || element.is(":checkbox")) {
                            element.closest('.option-group').after(error);
                   } else {
                            error.insertAfter(element.parent());
                   }
                }


        });

包含正确的 js 库,但它仍然无法验证。我做错了什么吗?

是的,正确,您必须用引号将复杂的名称括起来。

但是,正如您向我们展示的那样,您的代码正在运行:http://jsfiddle.net/7pmc6q9c/

....
    rules: {
        "customer-first_name": {
            required: true
        },
        "customer-last_name": {
            required: true
        },
        "customer-email": {
            required: true,
            email: true
        },
        "customer-telephone": {
            required: true
        }
    },
....
  • 您的 customer-first_namecustomer-last_name 字段已经在 HTML 中设置了值,因此除非用户将它们清空,否则无需验证。

  • 您的 customer-email 字段在 HTML 中不存在,因此验证将忽略它。

  • 您的 customer-telephone 字段正在按照您指定的方式进行验证。