Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

jquery validation form submit

$("#FromId").validate({
  submitHandler: function(form) {
   form.submit();
  }
});
Comment

jquery validation submit handler

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

jquery validate 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'],
    }
];
Comment

PREVIOUS NEXT
Code Example
Javascript :: xmlhttprequest get request 
Javascript :: //disable-linter-line 
Javascript :: how to make nextjs image component responsive 
Javascript :: jquery read query string 
Javascript :: is_int js 
Javascript :: vscode js brackets not close 
Javascript :: check the string is vowel or not javascript 
Javascript :: Changing the img src using jQuery. 
Javascript :: usereducer hook react 
Javascript :: get document jquery 
Javascript :: javascript merge objects 
Javascript :: javascript create an array of range between two numbers 
Javascript :: adb bootloader reboot 
Javascript :: refresh after delete in express 
Javascript :: js localstorage 
Javascript :: replace data in files in node.js 
Javascript :: redirect with javascript 
Javascript :: how to validate a string using regular expression in javascript 
Javascript :: handle error in promise chain 
Javascript :: scroll element by javascript 
Javascript :: Array.include is not a function javascript error help 
Javascript :: insert data from lambda to dynamodb 
Javascript :: jquery to br 
Javascript :: how to run curl in javascript 
Javascript :: localstorage remove item 
Javascript :: button onclick enter key 
Javascript :: uploadgetfiletypefileextension 
Javascript :: js toggle value 
Javascript :: how to get href value of anchor tag in jquery in list 
Javascript :: scrollbar automatically scroll down as new divs are added reactjs 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =