Skip to content Skip to sidebar Skip to footer

Jquery.form/validate Plugin Only Allow Submit If Input Is Changed

I'd like to use the jQuery.Form/Validate plugins to only allow my form to be submitted if any of inputs were actually changed. There is callback logic for this using beforeSubmit:

Solution 1:

First of all it will never show "Not Changed" since it returns false before it hits that part. You should filter out the submit button too since you probably aren't checking its value.

$($form+':input').not(':submit').each(function(index, this) {
    value = this.value;
    storedValue = $(this).data("stored");
    if (value != storedValue) {
        console.log("Changed");
        returntrue;      
    }
    else {       
        console.log("NOT changed");
        returnfalse;
    }
});

UPDATE With great assistance from @wirey, together we've (@timrpeterson and @wirey) put together a solution. Instead of returning true/false within the each() loop, I incremented a value ,totalChanged, and assessed after the each() loop whether or not it was greater than 0.

here's the code:

beforeSubmit: function(arr, $form){   
var value = '', storedValue='', totalChanged=0;
$('#myForm :input').not(':submit,:hidden').each(function (i, el) {
  //console.log($(this));console.log($($form));
    value=$(el).val(); 
    storedValue=$(el).data("stored");          
    if (value != storedValue) {  
      totalChanged++;    
    }
    else {     
    }
}); //eachif(totalChanged>0){
     console.log("at least one changed");
     returntrue;
  }
  else{
     console.log("All NOT changed");
     returnfalse;
  }
} //beforeSubmit

Post a Comment for "Jquery.form/validate Plugin Only Allow Submit If Input Is Changed"