1

Hello Im check through a form to see if the inputs have been filled out. I Keep the submit button disabled until the required fields are filled out. However if I type in the textarea which is required the button is enabled even if the previous required fields are not filled out.

Here is a fiddle https://jsfiddle.net/v2to0fcw/1/

here is the markup

<form action="/contact" method="post">
    <div class="form-row firstName">
        <p class="form-alert">Enter a valid first name.</p>
        <input type="text" name="firstName" id="firstName" placeholder="First Name*" required class="form-control required">
    </div>
    <div class="form-row lastName">
        <p class="form-alert">Enter a valid last name. Example: Smith</p>
        <input type="text" name="lastName" id="lastName" placeholder="Last Name" class="form-control">
    </div>
    <div class="form-row phone">
        <p class="form-alert">Enter a valid phone number. Example: 555 555 5555</p>
        <input type="tel" name="phone" id="phone" placeholder="Phone" class="form-control">
    </div>
    <div class="form-row email ">
        <p class="form-alert">Enter a valid email. Example: person@email.com</p>
        <input type="email" name="email" id="email" placeholder="Email*" required class="form-control required">
    </div>
    <div class="form-row">
        <input type="text" name="subject" id="subject" placeholder="Subject*" required class="form-control required">
    </div>
    <div class="form-row">
        <textarea name="message" id="message" placeholder="Message*" required class="form-control required"></textarea>
    </div>
    <input type="submit" name="submit" id="submit" value="Send" disabled="disabled" class="btn btn-primary pull-right">
</form>

here is the jquery

$(document).ready(function() 
{ 
    (function() 
    {
        $('.required').on('keyup change', function() 
        {
            $('.required').each(function() 
            {
                if (this.value == '') 
                {
                    $('#submit').attr('disabled', 'disabled'); // updated according to http://stackoverflow.com/questions/7637790/how-to-remove-disabled-attribute-with-jquery-ie
                } 
                else 
                {
                    $('#submit').removeAttr('disabled'); // updated according to http://stackoverflow.com/questions/7637790/how-to-remove-disabled-attribute-with-jquery-ie
                }
            });

        });
    })()

The button will not enable unless you fill out all required fields but if I skip the first three and type in the message the button is enabled. Whats going wrong?

badsyntax
  • 311
  • 4
  • 14
  • 1
    If the final `` is not an empty string then the button's `disabled` attribute is removed; which is exactly what your code is written to do. – David Thomas Sep 16 '16 at 23:35
  • by final input you mean textarea? So if (this.value == '') is only targeting final input? I also tried if ('.required' == '') – badsyntax Sep 16 '16 at 23:39
  • The problem is that you're adding and removing the attribute each time through the loop. So at the end, it contains the setting from the last iteration. – Barmar Sep 16 '16 at 23:47

4 Answers4

4

I've updated the fiddle:

https://jsfiddle.net/efbonrnr/1/

    $('.required').on('keyup change', function() 
    {
            var requiredFieldsCompleted = true;
        $('.required').each(function() 
        {
            if (this.value == '')
            {
                requiredFieldsCompleted = false;
            }
        });

        if (requiredFieldsCompleted)
        {
            $('#submit').removeAttr('disabled');

        }
        else
        {
            $('#submit').attr('disabled', 'disabled');
        }

    });

The issue was you were disabling / enabling the field after each check, without taking into the previous items. As such, only the last required field would affect the result.

NRaf
  • 7,407
  • 13
  • 52
  • 91
0

Hi we need to maintain a flag instead of adding and removing disabled attribute again and again, that is causing this issue see my comment with variable var flag;

$(document).ready(function() {
  (function() {
    $('.required').on('keyup change', function() {
      var flag = false; // we need to check if any of the field is empty then set it to true, we will decide the send button to be enabled or disabled after that
      $('.required').each(function() {
        if (this.value == '') {
          flag = true;
        }
      });
      if (flag) {
        $('#submit').attr('disabled', 'disabled');
      } else {
        $('#submit').removeAttr('disabled');
      }
    });
  })()

  $('#firstName').on('change', function() {
    var firstName = this.value;
    if (/^[a-zA-Z- ]*$/.test(firstName) == false) {
      $("#firstName").css('border', '1px solid #d22020').val('');
      $('.firstName .form-alert').css('display', 'block');
    } else {
      $("#firstName").css('border', 'none');
      $('.firstName .form-alert').css('display', 'none');
    }
  });

  $('#lastName').on('change', function() {
    var lastName = this.value;
    if (/^[a-zA-Z- ]*$/.test(lastName) == false) {
      $("#lastName").css('border', '1px solid #d22020').val('');
      $('.lastName .form-alert').css('display', 'block');
    } else {
      $("#lastName").css('border', 'none');
      $('.lastName .form-alert').css('display', 'none');
    }
  });

  $('#phone').on('change', function() {
    var phone = this.value;
    if (this.value !== '') {
      if (!$.isNumeric(phone)) {
        $("#phone").css('border', '1px solid #d22020').val('');
        $('.phone .form-alert').css('display', 'block');
      } else {
        $("#phone").css('border', 'none');
        $('.phone .form-alert').css('display', 'none');
      }
    }
  })

  $('#email').on('change', function() {
    var email = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
    if (!email.test(this.value)) {
      $("#email").attr('placeholder', 'johnsmith@email.com').css('border', '1px solid #d22020').val('');
      $('.email .form-alert').css('display', 'block');
    } else {
      $("#email").css('border', 'none').attr('placeholder', 'Email *');;
      $('.email .form-alert').css('display', 'none');
    }
  });
});
.form {
  float: left;
  width: 300px;
}
.form-row {
  float: left;
  width: 100%;
  margin: 0 0 20px 0;
}
.label-control {
  float: left;
}
.required-label:after {
  float: right;
  margin: 3px 0 0 3px;
  content: '\f069';
  color: rgba(245, 0, 0, 1);
  font-family: 'FontAwesome';
  font-size: 8px;
}
.form-control {
  float: left;
  width: 100%;
  max-width: 100%;
  padding: 9px 9px;
  background-color: rgba(245, 245, 245, 1);
  border: none;
  border-radius: 0px;
  box-shadow: inset 0px 0px 2px 1px rgba(0, 0, 0, 0.3);
  color: rgba(68, 68, 68, 1);
  font-family: 'Open Sans';
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 1px;
  -webkit-appearance: none;
  -webkit-transition: all .2s ease-in-out;
  -ms-transition: all .2s ease-in-out;
  transition: all .2s ease-in-out;
}
.form-control:hover,
.form-control:focus,
.form-control:active {
  box-shadow: inset 0px 0px 2px 1px rgba(0, 0, 0, 0.3);
}
textarea {
  min-height: 175px;
}
input[type=submit] {
  box-shadow: none;
  cursor: pointer;
  letter-spacing: 1px;
}
input[type=submit]:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.form-alert {
  float: left;
  display: none;
  width: 100%;
  color: rgba(210, 32, 32, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/contact" method="post" class="form">
  <div class="col-dual">
    <div class="form-row firstName">
      <label for="firstName" class="label-control required-label">First Name:</label>
      <p class="form-alert">Enter a valid first name.</p>
      <input type="text" name="firstName" id="firstName" placeholder="First Name*" required class="form-control required">
    </div>
    <div class="form-row lastName">
      <label for="lastName" class="label-control">Last Name:</label>
      <p class="form-alert">Enter a valid last name. Example: Smith</p>
      <input type="text" name="lastName" id="lastName" placeholder="Last Name" class="form-control">
    </div>
    <div class="form-row phone">
      <label for="phone" class="label-control">Phone:</label>
      <p class="form-alert">Enter a valid phone number. Example: 555 555 5555</p>
      <input type="tel" name="phone" id="phone" placeholder="Phone" class="form-control">
    </div>
    <div class="form-row email ">
      <label for="email" class="label-control required-label">Email:</label>
      <p class="form-alert">Enter a valid email. Example: person@email.com</p>
      <input type="email" name="email" id="email" placeholder="Email*" required class="form-control required">
    </div>
  </div>
  <div class="col-dual">
    <div class="form-row">
      <label for="subject" class="label-control required-label">Subject:</label>
      <input type="text" name="subject" id="subject" placeholder="Subject*" required class="form-control required">
    </div>
    <div class="form-row">
      <label for="message" class="label-control required-label">Message:</label>
      <textarea name="message" id="message" placeholder="Message*" required class="form-control required"></textarea>
    </div>
    <input type="submit" name="submit" id="submit" value="Send" disabled="disabled" class="btn btn-primary pull-right">
  </div>
</form>
Irfan Anwar
  • 1,878
  • 17
  • 30
0

https://jsfiddle.net/v2to0fcw/5/

Snippet:

 if (this.value == '') {
   $('#submit').attr('disabled', 'disabled');
   return false;
 } 

You are using validation(enable/disable submit button) over "required" class, which is used on multiple input elements. When you edit the last textbox, the validation runs over all input elements(which has required class) & the result of final element(which is text area in your case) alone is taken into account, which causes the issue.

msg
  • 1,480
  • 3
  • 18
  • 27
0

Updated the fiddle, https://jsfiddle.net/efbonrnr/9/

No need to run the whole loop when first empty field is found.

if (this.value == '') {

      requiredFieldsCompleted = false;          
      return false;
}
JVM
  • 946
  • 1
  • 10
  • 23