Hi jQuery validation only validate 1st element of array input
if you customize
checkForm: function() {
this.prepareForm();
for (var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++) {
if (this.findByName(elements[i].name).length != undefined && this.findByName(elements[i].name).length > 1) {
for (var cnt = 0; cnt < this.findByName(elements[i].name).length; cnt++) {
this.check(this.findByName(elements[i].name)[cnt]);
}
} else {
this.check(elements[i]);
}
}
return this.valid();} // this is not workig proper so you don't toch this code
but I saw this code also not working some scenario so that i find Tigger point of form summation after validation. if you want to validate custom
I share you trick you can customization you own validation then submit your form. code in below .
====================================================================
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = ( this.currentElements = this.elements() ); elements[ i ]; i++ ) {
this.check( elements[ i ] );
}
return this.valid();
}, // write as it is its woking now
====================================================================
$(document).ready(function($) {
$("#my_form").validate({
rules: {
"product_name": "required",
// "product_image[]": "required",
// password: {
// required: true,
// minlength: 6
// },
// city: "required",
// gender: "required"
},
messages: {
"product_name": "Please enter Product Name",
// "product_image[]": "required",
// password: {
// required: "Please provide a password",
// minlength: "Your password must be at least 6 characters long"
// },
// city: "Please enter your city",
// gender: "This field is required"
},
errorPlacement: function(error, element)
{
if ( element.is(":radio") )
{
error.appendTo( element.parents('.form-group') );
}
else
{ // This is the default behavior
error.insertAfter( element );
}
},
submitHandler: function(form) {
var values = $("input[name='product_image[]']").map(function(){return $(this).val();}).get();
for (let i = 0; i < values.length; i++) {
if(values[i] === "")
{
alert('Please select Image on position : '+(i+1));
return false;
}else{
}
}
console.log(values.includes(''));
if(values.includes('') === false);
{
form.submit();
}
}
});
});
form.submit(); is main Tigger so you can write customize validation code before this run within submitHandler function scope
I hope this solution is work for you