简体   繁体   English

Jquery 表单验证插件允许规则中需要的字段:为空

[英]Jquery Form validation plugin allows field that's required in rules: to be empty

I am using the jquery validation plugin to validate a form submission.我正在使用 jquery 验证插件来验证表单提交。 The validate is being triggered on submit and it runs but always returns the alert telling me 'valid form submitted', even though the fields are left blank in the form.验证在提交时被触发并运行,但总是返回警报,告诉我“已提交有效表单”,即使表单中的字段留空。 It is like it is just completely bypassing the rules: section.就好像它完全绕过了规则:部分。

Here is the jquery:这是jquery:

$(document).ready(function () {
var form =  $("#task-form");
form.validate({ 

    rules: {
        assigned_to_user_id: {
            required: true
        },
        sourceable_id: {
            required: true
        },
        sourceable_type: {
            required: true
        },
        asset_list: {
            required: true
        },
        assignment_date: {
            required: true
        },
        expenditure_type_id: {
            required: true
        },
        inventory_stock_location_id: {
            required: true
        },
        inventory_quantity: {
            required: true
        }
    },
     submitHandler: function (form) { // for testing
        alert('valid form submitted'); // for testing
        return false; // for testing
    }
});
});

The html of just the form section of the page is 350 lines of code so I'm not sure if it is appropriate to post that much here.页面表单部分的 html 是 350 行代码,所以我不确定在这里发布这么多是否合适。 I'm hoping that my error is in the jquery code and someone can see it.我希望我的错误在 jquery 代码中,有人可以看到它。

You have to add "name" attribute to your input fields on which you want validation to work and the "name" attribute should match the rules .您必须将"name"属性添加到您希望验证工作的输入字段中,并且"name"属性应与rules匹配。

<input type="text" class="form-control" id="inputUsername" name="inputUsername" placeholder="What you wanna know as?...."  >


rules: {
     inputUsername: {
        required: true,
        minlength: 4,
        maxlength: 10
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM