Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery disable submit button based on status array
    text
    copied!<p>I'm trying to make my own custom form validation. I want to maintain a status array with each field having it's own key : value (value is either "good" or "bad"). When the array contains a "bad", I want to disable the submit button.</p> <p>Error/Success messages work next to each field (my ajax works fine), but I can't get the submit button to disable.</p> <pre><code>$(function() { // Custom Validation var statusArray = { 'email' : 'good', 'passC' : 'good', 'pass1' : 'good', 'pass2' : 'good' } $("#email").blur(function() { var form_data = {email: $("#email").val()} $.ajax({ url: "&lt;?= base_url('lounge/validation/email'); ?&gt;", type: 'POST', data: form_data, success: function(msg) { if (msg['status'] == false){ $('#email_status').html('&lt;span class="lounge_error"&gt;'+msg['message']+'&lt;/span&gt;'); statusArray['email'] = 'bad'; } else { $('#email_status').html('&lt;span class="lounge_okay"&gt;'+msg['message']+'&lt;/span&gt;'); statusArray['email'] = 'good'; } } }); statusCheck(); return false; }) // Password fields validation $('#change_password').change(function () { if ($(this).attr("checked")) { $('#current_password_status').html('').css({"display": "inline-block"}); $('#new_password1_status').html('').css({"display": "inline-block"}); $('#new_password2_status').html('').css({"display": "inline-block"}); $("#current_password").blur(function() { var form_data = {password: $("#current_password").val()} $.ajax({ url: "&lt;?= base_url('lounge/validation/password'); ?&gt;", type: 'POST', data: form_data, success: function(result) { if (result['status'] == false){ $('#current_password_status').html('&lt;span class="lounge_error"&gt;'+result['message']+'&lt;/span&gt;'); statusArray['passC'] = 'bad'; } else { $('#current_password_status').html('&lt;span class="lounge_okay"&gt;'+result['message']+'&lt;/span&gt;'); statusArray['passC'] = 'good'; } } }); statusCheck(); return false; }); $("#new_password1").blur(function() { var form_data = {pass1: $("#new_password1").val()} $.ajax({ url: "&lt;?= base_url('lounge/validation/newpass1'); ?&gt;", type: 'POST', data: form_data, success: function(result) { if (result['status'] == false){ $('#new_password1_status').html('&lt;span class="lounge_error"&gt;'+result['message']+'&lt;/span&gt;'); statusArray['pass1'] = 'bad'; } else { $('#new_password1_status').html('&lt;span class="lounge_okay"&gt;'+result['message']+'&lt;/span&gt;'); statusArray['pass1'] = 'good'; } } }); statusCheck(); return false; }); $("#new_password2").blur(function() { var form_data = {pass2: $("#new_password2").val()} $.ajax({ url: "&lt;?= base_url('lounge/validation/newpass2'); ?&gt;", type: 'POST', data: form_data, success: function(result) { if (result['status'] == false){ $('#new_password2_status').html('&lt;span class="lounge_error"&gt;'+result['message']+'&lt;/span&gt;'); statusArray['pass2'] = 'bad'; } else { $('#new_password2_status').html('&lt;span class="lounge_okay"&gt;'+result['message']+'&lt;/span&gt;'); statusArray['pass2'] = 'good'; } } }); statusCheck(); return false; }); } else { $('#current_password_status').html('').css({"display": "none"}); $('#new_password1_status').html('').css({"display": "none"}); $('#new_password2_status').html('').css({"display": "none"}); } }); }); function statusCheck() { var status = []; $.each(statusArray, function(key, value) { status.unshift(value); }); if ($.inArray('bad', status) == '-1') { $("button[type=submit]").removeAttr("disabled"); } else { $("button[type=submit]").attr("disabled", true); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload