“JQuery Validasi Submithandler” Kode Jawaban

JQuery Validasi Kirim Handler

$("#myform").validate({
  submitHandler: function(form) {
    $(form).ajaxSubmit();
  }
});
Ahmed

JQuery Validasi Submithandler


function submithandler() {
	var errors = [];
	formFields.forEach( (field) => {
    	
        // loop to check all rules attached to the fields
        field.rules.forEach( (rule) => {
        
        	if (rule == 'required' && ! $(`#${field.id}`).val().length) {
            	errors.push(`${field.id} is required!`)
            } else if (rule.includes('max:')) {
            	let limit = rule.split(':')[1]; // this will get the number attached to the max rule
                if (limit > $(`#${field.id}`).val().length ) {
                	errors.push(`maximum length of charater must be `${limit})
                }
            }
        
        })
        
    })
    
    if (errors.length) {
      
     	return errors;
    }
  
  //do ajax post here
  
  
  
}


// edit the code below base on what u need

var formFields = [
	{
    	id:'name', // use to access the dom.eg $('#name')
        rules:['required']
    },
    {
    	id:'phone',
        rules:['required', 'max:11'],
    }
];
elezerk

Jawaban yang mirip dengan “JQuery Validasi Submithandler”

Pertanyaan yang mirip dengan “JQuery Validasi Submithandler”

Lebih banyak jawaban terkait untuk “JQuery Validasi Submithandler” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya