2

I have a Bootstrap form and I am basically trying to loop through all input fields, check if they have a required class and their value is null, if so add some CSS as an error indicator. For some reason the if statement is not working.

My code is:

$("#orderForm").on("submit", function(e) {
var formInputFields = $(".form-control")
formInputFields.each(function(i) {
  if (formInputFields[i].hasClass("required") && formInputFields[i].val() === "null") {
    e.preventDefault();
    formInputFields.css("border", "2px solid #741e4f");
  }
});

});

HTML is along these lines, but longer, same structure though, the form-group div is repeated:

<form class="form-horizontal" id="orderForm">
            <div class="form-group">
              <label for="" class="col-sm-4 control-label">Lorem</label>
              <div class="col-sm-6">
                <select name="" id="" class="form-control required">
                  <option value=""></option>
                  <option value="lorem">lorem</option>
                  <option value="ipsun">ipsun</option>
                </select>
              </div>
            </div>

3
  • add a semicolon var formInputFields = $(".form-control"); Commented Jun 15, 2016 at 9:43
  • @ Hi JSamarjiev, why do you not try - JS Validator for form validation. Check out this link:: 1000hz.github.io/bootstrap-validator Commented Jun 15, 2016 at 9:53
  • 1
    Don't re-invent the wheel - jqueryvalidation.org Commented Jun 15, 2016 at 10:02

3 Answers 3

1

Your code threw error when you say formInputFields[i].hasClass because formInputFields was a jquery object but not formInputFields[i]. Since you were using $.each, the 2nd parameter would have given you current element and instead of formInputFields[i], you could just use that 2nd parameter. Also, when you say cur.val()==="null" it will be strong comparison between left and right i.e. both should match by value and type which always failed here. So just check the length of val and if it is 0 that will be invalid element. Below are the changes I've made. Check and it and let me know if you face any issues.

$("#orderForm").on("submit", function(e) {
  var formInputFields = $(".form-control")
  formInputFields.each(function(i, v) {
    var cur = $(v);
    if (cur.hasClass("required") && !(cur.val().length)) {
      e.preventDefault();
      cur.css("border", "2px solid #741e4f");
    }
    else
      cur.css("border", "");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<form class="form-horizontal" id="orderForm">
  <div class="form-group">
    <label for="" class="col-sm-4 control-label">Lorem</label>
    <div class="col-sm-6">
      <select name="" id="" class="form-control required">
        <option value=""></option>
        <option value="lorem">lorem</option>
        <option value="ipsun">ipsun</option>
      </select>
    </div>
  </div>
  <input class="btn btn-info" type="submit" value="Submit" />
</form>

Sign up to request clarification or add additional context in comments.

2 Comments

Just out of curiosity, what is the thinking behind cur syntactically?
I would expand it as current... :)
0

Firstly, when an input is required add required as an attribute rather than a class.

<input type="text" id="foo" placeholder="bar" value="" required>

Then you will be able to get any required inputs currently missing values using:

var list_of_invalid_inputs = document.querySelectorAll('*:invalid');

or if you must use jquery like so.

The same selector works in css too:

input:invalid { border: 2px solid #741e4f; }

Comments

0

you can use $(this) instead of formInputFields[i].hasClass... as you need the object to verify if it has the "required" class or not and if it has value or not, for example:

$(document).ready(function(){
  $("#orderForm").on("submit", function(e){
        var formInputFields = $(".form-control");
    formInputFields.each(function(i) {
        if($(this).hasClass("required") && $.trim($(this).val()) === ""){
        e.preventDefault();
            $(this).css("border", "2px solid #741e4f");
      }
    });
    return false;
  });
});

JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.