0

Here is my my form input: I will have multiple, i want to validate all dynamically!

  <input type="number" name="bill[1][amount]" class="form-control" placeholder="Enter bill amount">

And here is my jquery code:

$("#bulkForm").validate({
            rules:
                 {
                  "bill[][amount]":
                          {
                             number: true,
                             required: true
                          }
                 },

                 submitHandler: function(form) {

                    var params = $("#bulkForm").serialize();

 });
3
  • Hi @Wiki Babu, pls upload your full form, i will solve it
    – jvk
    Dec 7, 2019 at 15:24
  • Please read minimal reproducible example to complete your question, what you tried, and what you are blocked on, the error or issue you encounter, ...
    – el-teedee
    Dec 7, 2019 at 15:25
  • jsfiddle.net/Yy2gB/10
    – jvk
    Dec 7, 2019 at 15:27

1 Answer 1

1

Here is solution, if you've dynamic input and validate each input

$(function(){
	$("#bulkForm").validate();
        $("[name^=bill]").each(function(){
    	$(this).rules("add", {
         	required: true,
       		number: true,
       		messages: {
            	required: "Please enter number"
       			}
     		});   
   		});
   		})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.js"></script>
  <form action="" id="bulkForm" >
		  <input type="text" name="bill[1][amount]" class="form-control" placeholder="Enter bill amount">
		  <br><input type="text" name="bill[2][amount]" class="form-control" placeholder="Enter bill amount">
		  <br>
	<input type="submit" name="submit" value="Submit">
	</form>
	

1
  • Thank you but i'm wondering where can i submit the form. Please add submitHandler code as well!
    – WK5
    Dec 8, 2019 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.