3

I created a form using Bootstrap and I need that the outline of inputs of class "partIVAInput" becomes red and that red feedback message appears - not only when input are empty - but also when their value - checked by the "isValidPIVA(value)" function - is wrong. The handler of "keyup blur change" for the input with class "partIVAInput" checks if the value and assign accordingly the classes "is-valid/invalid" to the input.

<!doctype html>
<html lang="it">
  <head>    
    <!-- Required meta tags -->
    <meta charset="ISO-8859-15">
    <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.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

    <script>
      $(function () {
        $(".numericInput, .partIVAInput, .capInput").bind("keyup blur change", function (event) {
          var value = $(this).val();
          if ($(this).hasClass("partIVAInput"))
            if (!isValidPIVA(value)) {
              $(this).removeClass("is-valid").addClass("is-invalid");
            }
            else {
              $(this).removeClass("is-invalid").addClass("is-valid");
            }
        });
      });
      //------------------------------------------------------------------------
      function isValidPIVA(value) {
        return (value === "123" ? true : false);
      }
      //------------------------------------------------------------------------
    </script>    
    <title>Test</title>
  </head>
  <body>
    <div class="container">
      <form id="frmIscrCant" class="needs-validation" novalidate="">              
        <div id="divDatiCommittente" class="card bg-light">
          <h5 class="card-header">Dati</h5>
          <div class="card-body">          
            <div class="form-row">  
              <div class="form-group col-md-8">            
                <label for="tbEnte">Denominazione</label>
                <input type="text" id="tbEnte" name="Ente" class="form-control" required=""> 
              </div>
            </div>    
            <div class="form-row">
              <div class="form-group col-md-6">
                <label for="tbPartIvaCommitt">Partita IVA</label>
                <input type="text" id="tbPartIvaCommitt" name="PartIvaCommitt" class="form-control partIVAInput" required=""> 
                <div class="invalid-feedback">Inserire una partita IVA valida (123)!</div>                  
              </div>
            </div>                         
          </div>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    </div>
    <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>             

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

  </body>
</html>

Perhaps I'm not handling things well since I'm experiencing the following problem.

When I insert in the field a right value (OK)... enter image description here

When I insert in the field a wrong value (OK)... enter image description here

When I submit with a wrong value in the field (NO!!! The feedback message is correctly displayed but the outline of the input has become green while it must be red. Also if I write a new wrong value in the field the outline of the input continues to be green. Further, the form is submitted while with a field marked as "is-invalid" I guess it shouldn't)... enter image description here

What's wrong?

PS I created this new thread in replacement of this How can I validate a field using a JavaScript function? that can be closed/deleted.

Flattit82
  • 63
  • 2
  • 9
  • You might find it easier (since you're already using jQuery) to use jQuery Validate + some custom rules to match the Bootstrap classes. For example: https://stackoverflow.com/questions/52426171/jquery-validate-form-inputs-highlight-labels-with-bootstrap-4 – Tieson T. Jun 11 '19 at 22:33
  • your issue is not present in your code, please correct your code first – Nisharg Shah Jun 12 '19 at 02:15
  • I replaced the pieces of code with a full working example. Just copy and paste the code in a HTML page and run it. In this example the only accepted value for the input "Partita IVA" is 123. I hope that now you can see the issue. Thanks – Flattit82 Jun 12 '19 at 07:32

0 Answers0