jQueryvalidation,在两个空白字段中,必须填充至少一个字段或两者都填写

我想validation我的表单,以便在两个空白字段中,至少必须填写一个字段,并且还可以填充两个字段; 但不能留下任何字段空白。

我正在使用jquery-1.9.1-min.js,这是我的html页面。

You need to type an email address
OR

任何建议怎么做….?

我已经试过了

  jQuery.validator.addMethod("require_from_group", function(value, element, options) { alert("xxx"); var valid = $(options[1], element.form).filter(function() { return $(this).val(); }).length >= options[0]; if(!$(element).data('reval')) { var fields = $(options[1], element.form); fields.data('reval', true).valid(); fields.data('reval', false); } return valid; }, jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields.")); 

仍然没有获得惨淡的输出。

您正在尝试使用validator.addMethod ,它是jQuery Validate插件的一部分 。 如果你还没有,你需要在你的代码中包含这个插件。

然后使用require_from_group规则,该规则已经是Validate插件的additional-methods.js文件的一部分 。 (不要忘记包含additional-methods.js文件。)

 rules: { myfieldname: { require_from_group: [1, ".class"] } } 
  • 第一个参数是要求的项目数。
  • 第二个参数是分配给分组中所有元素的class 。 我在你的两个输入元素中添加了一个send类。

  • 还可以使用groups选项将两条消息合并为一条消息。

jQuery

 $(document).ready(function () { $('#forgot_pass_form').validate({ // initialize the plugin groups: { // consolidate messages into one names: "uname email" }, rules: { uname: { require_from_group: [1, ".send"] }, email: { require_from_group: [1, ".send"] } } }); // for your custom message jQuery.extend(jQuery.validator.messages, { require_from_group: jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields.") }); }); 

工作演示: http : //jsfiddle.net/sgmvY/1/


编辑 : 根据Github, require_from_group方法存在一个未解决的问题 。 在修复之前,开发人员会在下面推荐此解决方案。 由于您将手动将修改后的方法添加到代码中,因此无需包含additional-methods.js文件。

新工作演示: http : //jsfiddle.net/kE7DR/2/

 $(document).ready(function () { jQuery.validator.addMethod("require_from_group", function (value, element, options) { var numberRequired = options[0]; var selector = options[1]; var fields = $(selector, element.form); var filled_fields = fields.filter(function () { // it's more clear to compare with empty string return $(this).val() != ""; }); var empty_fields = fields.not(filled_fields); // we will mark only first empty field as invalid if (filled_fields.length < numberRequired && empty_fields[0] == element) { return false; } return true; // {0} below is the 0th item in the options field }, jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields.")); $('#forgot_pass_form').validate({ // initialize the plugin groups: { names: "uname email" }, rules: { uname: { require_from_group: [1, ".send"] }, email: { require_from_group: [1, ".send"] } } }); }); 

@Sparky我正在尝试使用您的答案来validation帐户名称和/或密码的更新。 我输入原始帐户名和密码,然后单击更新按钮并执行原始帐户名和密码的validation(即,没有消息说必须输入新的帐户或密码)。 我的代码是:

 $(document).ready(function(){ $.validator.addMethod( "regex", function(value, element, regexp) { if (regexp.constructor != RegExp) regexp = new RegExp(regexp); else if (regexp.global) regexp.lastIndex = 0; return this.optional(element) || regexp.test(value); }, "Please enter correct Characters." ); jQuery.validator.addMethod("require_from_group", function (value, element, options) { var numberRequired = options[0]; var selector = options[1]; var fields = $(selector, element.form); var filled_fields = fields.filter(function () { // it's more clear to compare with empty string return $(this).val() != ""; }); var empty_fields = fields.not(filled_fields); // we will mark only first empty field as invalid if (filled_fields.length < numberRequired && empty_fields[0] == element) { return false; } return true; // {0} below is the 0th item in the options field }, jQuery.format("'Please enter either a new Account name and/or a new password'/Please fill out at least {0} of these fields.")); $('[data-toggle="tooltip"]').tooltip(); $("#contactForm").validate({ groups: { // consolidate messages into one names: "accountName1 enterPassword1" }, rules: { accountName: { required: true, minlength: 2 }, enterPassword: { required: true, minlength: 8 }, accountName1: { require_from_group: [1, ".send"], minlength: 2 }, accountName2: { minlength: 2, equalTo: "#accountName1" }, enterPassword1: { require_from_group: [1, ".send"], regex: /^(?=.*[az])(?=.*[AZ])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}/, minlength: 8 }, enterPassword2: { regex: /^(?=.*[az])(?=.*[AZ])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}/, minlength: 8, equalTo: "#enterPassword1" } }, messages: { accountName: { required: "Please enter your current account name.", minlength: "Your account name must consist of at least 2 characters." }, enterPassword: { required: "Please enter your current password.", minlength: "Your password must consist of at least 8 characters." }, accountName1: { minlength: "Your account name must consist of at least 2 characters." }, accountName2: { minlength: "Your account name must consist of at least 2 characters.", equalTo: "Your confirmation account name does not match the original." }, enterPassword1: { regex: "Please nter at least 8 characters containing at least 1 lower case, 1 upercase, 1 special and 1 numeric..", minlength: "Your password must consist of at least 8 characters." }, enterPassword2: { regex: "Please enter at least 8 characters containing at least 1 lower case, 1 upercase, 1 special and 1 numeric..", minlength: "Your password must consist of at least 8 characters.", equalTo: "Your confirmation password does not match the original." } }, submitHandler : function(contactForm) { //do something here var frm = $('#contactForm'); //alert($("#accountName1").val()); $.ajax({ type: "POST", url: "UpdateAccountView", cache: false, data: frm.serialize(), success: function(data){ console.log('Submission was successful.'); console.log(data); $("#accountName").focus(); $('#ajaxGetUserServletResponse').text(data); } }); } }); }); // end document.ready