Display name
Display name

Reputation: 1542

The required attribute is not working in Chrome but does work in FireFox

I'm using bootstrap for styling and I've not defined any of my own classes. Since FireFox is working, I believe that the structure I'm using is correct. But Chrome is not happy, and I don't know enough about the nuances between the two to track it down. The required attribute for the 'State' dropdown is not working in either, but I figure that's another question.

I came across an article describing how Chrome won't load scripts that are non ssl when the application url is ssl, but this should not apply as I'm using ng serve and my url is insecure. Otherwise, all articles related to the required attribute not working have to do with failures on FireFix, which is the opposite condition that I'm experiencing.

FireFox: 86.0.1 (64-bit)
Chrome:  89.0.4389.90 (Official Build) (64-bit)

Visual depiction of difference between browser renderings

From the component html

<div class="row">
  <div class="col-md-5 mb-3">
    <label for="{{idPrefix}}City">City</label>
    <input type="text" class="form-control" id="{{idPrefix}}City" placeholder="" required [(ngModel)]="model.City">
  </div>
  <div class="col-md-4 mb-3">
    <label for="{{idPrefix}}state">State</label>
    <select class="custom-select d-block w-100" id="{{idPrefix}}state" required [(ngModel)]="model.State">
      <option value="">Choose...</option>
      <option *ngFor="let State of StateArray" value="{{State.abbreviation}}">{{State.name}}</option>
    </select>
  </div>
  <div class="col-md-3 mb-3">
    <label for="{{idPrefix}}zip">Zip</label>
    <input type="text" class="form-control" id="{{idPrefix}}zip" placeholder="" required [(ngModel)]="model.Zip">
  </div>
</div>

index.html

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

Upvotes: 0

Views: 1641

Answers (2)

hrtj123
hrtj123

Reputation: 11

Check the code below it works for me in all browsers

    <!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Test</title>
</head>
<body>
<form class="needs-validation" novalidate>
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom01">First name</label>
      <input type="text" class="form-control" id="validationCustom01" placeholder="First name" value="Mark" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationCustom02">Last name</label>
      <input type="text" class="form-control" id="validationCustom02" placeholder="Last name" value="Otto" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationCustomUsername">Username</label>
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend">@</span>
        </div>
        <input type="text" class="form-control" id="validationCustomUsername" placeholder="Username" aria-describedby="inputGroupPrepend" required>
        <div class="invalid-feedback">
          Please choose a username.
        </div>
      </div>
    </div>
  </div>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationCustom03">City</label>
      <input type="text" class="form-control" id="validationCustom03" placeholder="City" required>
      <div class="invalid-feedback">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationCustom04">State</label>
      <input type="text" class="form-control" id="validationCustom04" placeholder="State" required>
      <div class="invalid-feedback">
        Please provide a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationCustom05">Zip</label>
      <input type="text" class="form-control" id="validationCustom05" placeholder="Zip" required>
      <div class="invalid-feedback">
        Please provide a valid zip.
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
      <label class="form-check-label" for="invalidCheck">
        Agree to terms and conditions
      </label>
      <div class="invalid-feedback">
        You must agree before submitting.
      </div>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
  'use strict';
  window.addEventListener('load', function() {
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation = Array.prototype.filter.call(forms, function(form) {
      form.addEventListener('submit', function(event) {
        if (form.checkValidity() === false) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();
</script>


</body>

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>

You can refer to the following documentation: https://getbootstrap.com/docs/4.0/components/forms/?#validation

Upvotes: 0

aarondiel
aarondiel

Reputation: 829

normally chrome does not create this red border for invalid inputs. you could add some css to crete that border manually:

input:required {
  border-color: #800000;
  border-width: 3px;
}

input:invalid {
  background-color: #ffdddd;
}

input:valid {
  background-color: #ddffdd;
}

but i honestly don't know how you would do that using bootsrap alone.

Upvotes: 1

Related Questions