skip to Main Content

I am currently using jQuery validation to validate my fields. I’ve two fields,
named “comments” & “account name“. Both fields have the same rule method where required is true. When I click the “save” button, only the account name was validated. Why is that so? Here is a screenshot of my problem and my codes

enter image description here

$(document).ready(function() {
  $.validator.setDefaults({
    errorClass: 'help-block',
    highlight: function(element) {
      $(element)
        .closest('.form-group')
        .addClass('has-error');

    },
    unhighlight: function(element, errorClass, validClass) {
      $(element)
        .closest('.form-group')
        .removeClass('has-error')
        .addClass('has-success');
    },
  });

  $('#dataForm').validate({
    rules: {
      commentInput: {
        required: true
      },
      accountNameInput: {
        required: true
      }
    },
    submitHandler: function(form) {
      alert('success');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.min.js"></script>
<form id="dataForm" method="post" action="#">

  <div class="form-group">
    <label class="control-label" for="commentInput">Comments</label>
    <textarea class="commentInput" id="commentInput" cols="20" rows="5"></textarea>
  </div>

  <div class="form-group">
    <label class="control-label" for="accountNameInput">Account name</label>
    <input type="text" id="accountNameInput" name="accountNameInput" placeholder="Account name" class="form-control font-bold" value="" />
  </div>


  <input type="submit" class="btn btn-primary" value="Save" id="saveButton" />

</form>

3

Answers


  1. You have to give all form fields that need validation a name attribute. That’s where the validation plugin gets the reference to the element from.

    From the documentation:

    Throughout the documentation, two terms are used very often, so it’s
    important that you know their meaning in the context of the validation
    plugin:

    method: A validation method implements the logic to validate an element, like an email method that checks for the right format of a
    text input’s value. A set of standard methods is available, and it is
    easy to write your own.

    rule: A validation rule associates an element with a validation method, like “validate input with name “primary-mail” with
    methods “required” and “email”.

    The name attribute is also required to be present on any form field that will need to transmit its data as part of the form submission.

    $(function() {
      $.validator.setDefaults({
        errorClass: 'help-block',
        highlight: function(element) {
          $(element)
            .closest('.form-group')
            .addClass('has-error');
        },
        unhighlight: function(element, errorClass, validClass) {
          $(element)
            .closest('.form-group')
            .removeClass('has-error')
            .addClass('has-success');
        },
      });
    
      $('#dataForm').validate({
        rules: {
          commentInput: {
            required: true
          },
          accountNameInput: {
            required: true
          }
        },
        submitHandler: function(form) {
          alert('success');
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.min.js"></script>
    <form id="dataForm" method="post" action="#">
    
      <div class="form-group">
        <label class="control-label" for="commentInput">Comments</label>
        <textarea class="commentInput" id="commentInput" name="commentInput" cols="20" rows="5"></textarea>
      </div>
    
      <div class="form-group">
        <label class="control-label" for="accountNameInput">Account name</label>
        <input type="text" id="accountNameInput" name="accountNameInput" placeholder="Account name" class="form-control font-bold" value="" />
      </div>
    
    
      <input type="submit" class="btn btn-primary" value="Save" id="saveButton" />
    
    </form>
    Login or Signup to reply.
  2. The validation plugin targets by the name attribute:

    <textarea id="commentInput" name="commentInput" cols="20" rows="5"></textarea>

    Login or Signup to reply.
  3. You need use the name attribute for validate.

    $(document).ready(function() {
      $.validator.setDefaults({
        errorClass: 'help-block',
        highlight: function(element) {
          $(element)
            .closest('.form-group')
            .addClass('has-error');
    
        },
        unhighlight: function(element, errorClass, validClass) {
          $(element)
            .closest('.form-group')
            .removeClass('has-error')
            .addClass('has-success');
        },
      });
    
      $('#dataForm').validate({
        rules: {
          commentInput: {
            required: true
          },
          accountNameInput: {
            required: true
          }
        },
        submitHandler: function(form) {
          alert('success');
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.min.js"></script>
    <form id="dataForm" method="post" action="#">
    
      <div class="form-group">
        <label class="control-label" for="commentInput">Comments</label>
        <textarea name="commentInput" class="commentInput" id="commentInput" cols="20" rows="5"></textarea>
      </div>
    
      <div class="form-group">
        <label class="control-label" for="accountNameInput">Account name</label>
        <input type="text" id="accountNameInput" name="accountNameInput" placeholder="Account name" class="form-control font-bold" value="" />
      </div>
    
    
      <input type="submit" class="btn btn-primary" value="Save" id="saveButton" />
    
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search