Jquery validate validates the form fields that are already in the DOM as well as fields that are added dynamically. But if the fields that are NOT dynamically added pass validation, the form submits ignoring the dynamically added fields.
Here is my code:
$('#milestone-form').validate({ // initialize the plugin
rules: {
"milestone[]": {
required: true,
minlength: 3,
},
"deadline[]": {
required: true,
minlength: 3,
},
"amount[]": {
required: true,
minlength: 2,
},
},
submitHandler: function(form) {
var data = $("#milestone-form").serialize();
$.ajax({
type:"POST",
url:"ajax/scripts/crt_ms.php",
data:data,
success:function(data){
if(!data){alert("Something went wrong")}else{alert("Success")}
$(document).off();
},
});
},
});
//Validation End
$(document).on('click', ".add", function(){
$(flieds).insertAfter($(this).closest("#fields"));
$('.field').each(function() {
$(this).rules('add', {
required: true,
});
});
});
$(".save-ms").click(function(){
if($('#milestone-form').valid()){
//$("#milestone-form").submit();
alert("POSTED");
return false;
} else { alert("ERROR");}
});
All my <input>
elements have a class=".field"
attribute.
Also, I noticed one thing that all the dynamic fields do not get a error LABEL instead only have a class to define it as invalid.
I have been trying the whole day to do this but just not happening.