Can we use wildcard with jquery validator

不问归期 提交于 2019-12-20 05:14:46

问题


when element name is not fixed then can we use wild card there for specifying name.

below code will work

<script type="text/javascript">
$(document).ready(function () {


    $('#myform').validate({
    rules: {
        $("[name^=test]"): {
        required: true,
        maxlength: 1
        }
    },
    messages: {
        $("[name^=test]"): {
        required: "You must check at least 1 box",
        maxlength: "Check no more than {0} boxes"
        }
    },
    submitHandler: function (form) { // for demo
        alert('valid form submitted'); // for demo
        return false; // for demo
    }
    });


});
    </script>
</head>
<body> 
<form id="myform" runat="server"> 
<input type="checkbox" id="chk1" name="test_1_p1" class="clschk" />x 
<input type="checkbox" id="chk2" name="test_2_p2" class="clschk"/>y 
<input type="checkbox" id="chk3" name="test_3_p3" class="clschk"/>z 
<input id="age" type="text" name="age" /> <input type="submit" /> 
</form> 
</body> 
</html>

回答1:


I assume the best way to achieve this goal is to consider:

  1. the parameters of validate (rules and messages) are json objects.
  2. a json object can be created at runtime if it must have dymanic content
  3. you need a custom rule to detect the max number of selected check boxes
  4. you need to define a function to define the corresponding error message (the max value cannot be written in more than one place to avoid confusion and so side effects).

So, a possible solution for your problem can be:

// global variable to save the validator object
var validator;


// a new rule to test if the selected checkboxes are more than the max or less than one
$.validator.addMethod('checkboxMax', function (value, elem, param) {
  var cacheCheckedElements = $('[name^=test]:checked');
  if (cacheCheckedElements.length < 1 || cacheCheckedElements.length > param.max) {
    return false;
  } else {
    validator.resetForm();
    return true;
  }
});


$(function () {
  // on ready: create the two json object: rules and messages
  var myRules = {};
  var myMessages = {};
  $('[name^=test]').each(function (index, element) {
    myRules[element.name] = {
      checkboxMax: {
        max: 2
      }
    };
    myMessages[element.name] = {
      checkboxMax: function(params, element) {
        return 'Check no more than ' + params.max + ' boxes ';
      }
    };
  });
  
  // use the previous fields (myRules and myMessages) as arguments
  validator = $('#myform').validate({
    rules: myRules,
    messages: myMessages,
    submitHandler: function (form) { // for demo
      alert('valid form submitted'); // for demo
      return false; // for demo
    }
  });
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>

<form id="myform" runat="server">
    <input type="checkbox" id="chk1" name="test_1_p1" class="clschk"/>x
    <input type="checkbox" id="chk2" name="test_2_p2" class="clschk"/>y
    <input type="checkbox" id="chk3" name="test_3_p3" class="clschk"/>z
    <input id="age" type="text" name="age"/> <input type="submit"/>
</form>


来源:https://stackoverflow.com/questions/36316776/can-we-use-wildcard-with-jquery-validator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!