I have the following form. I am trying to validate the inputs using jquery validation plugin. I have tried some codes but its not working. When I click on the submit button it just goes to form's action.<form name="voucherform" action="something" method="post"> <input type="text" name="reg_number[]" value="" /> <input type="text" name="reg_number[]" value="" /> <input type="text" name="reg_number[]" value="" /> <input type="submit" name="submit" value="submit" />Could you please show me how to validate the above form using jquery validation plugin?Thanks :)My Jquery code: <script type="text/javascript" src="<?php echo base_url();?>support/datepicker/js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="<?php echo base_url();?>support/js/jquery.validate.js"></script> <script> $(document).ready(function() {// validate signup form on keyup and submitvar validator = $("#voucherform").validate({ showErrors: function(errorMap, errorList) { $(".errormsg").html($.map(errorList, function (el) { return el.message; }).join(", ")); }, wrapper: "span", rules: { reg_number[]: { required: true, minlength: 2, remote: { url: '<?php echo base_url();?>sales/invoice_check', async: false, type: 'post' }, } }, messages: { reg_number[]: { required: "Enter Reg Number", minlength: jQuery.format("Enter at least {0} characters"), remote: jQuery.format("{0} is already in use") }, } }); }); </script> 解决方案 You have two problems:You aren't selecting the form element properly.You're looking for a form with id "voucherform" and your markup does not contain one. You probably want to change your selector to:$("form[name='voucherform']").validate({ ... });Brackets ([]) are not valid in JavaScript identifiers. This means that your script is not being parsed correctly. To resolve this, just wrap those fields that have brackets in them in quotes, i.e. 'reg_number[]' instead of reg_number[].Tweaked validate code:var validator = $("form[name='voucherform']").validate({ showErrors: function(errorMap, errorList) { $(".errormsg").html($.map(errorList, function(el) { return el.message; }).join(", ")); }, wrapper: "span", rules: { 'reg_number[]': { required: true, minlength: 2, remote: { url: '<?php echo base_url();?>sales/invoice_check', async: false, type: 'post' } } }, messages: { 'reg_number[]': { required: "Enter Reg Number", minlength: jQuery.format("Enter at least {0} characters"), remote: jQuery.format("{0} is already in use") } }});Example: http://jsfiddle.net/hfrhs/ 这篇关于使用jQuery验证插件验证数组输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 23:37