skip to Main Content
<form action="" method="POST" id="form" >
<select id="inputCustomerType" class="form-control" name="customer_type" required>
     <option value="">Choose...</option>
     <option value="select1">Select1</option>
     <option value="select2">Select2</option>
     <option value="select3">Select3</option>
 </select>
 <script>
    $('#form').parsley();
  </script>
 <button type="submit" id="updateButton" class="btn btn-primary mt-3" >Update</button>

</form>

i want,
if i change the select option, remove the previous parsley error display messages
and then i click update again check parsley validation and displaying particular field errors

2

Answers


  1. I just changed the button type to "button" to prevent the form from submitting automatically when clicked.
    When the button is clicked, we call form.reset() to clear any previous error messages from the form.
    Then, we call form.validate() to trigger Parsley validation for the form again. Parsley will display error messages for the currently selected field only.

    $(document).ready(function() {
            var form = $('#form').parsley();
    
            // Handle click event on the "Update" button
            $('#updateButton').on('click', function() {
                // Clear previous error messages
                form.reset();
    
                // Validate the form
                form.validate();
            });
        });
    
    Login or Signup to reply.
  2. Below solution work for me, you can check this. you need to handle select change event ‘inputCustomerType’

    $(document).ready(function() {
    var form = $(‘#myForm’);

       // Initialize Parsley validation
       form.parsley();
    
       // Handle select change event
       $('#inputCustomerType').on('change', function() {
          // Remove previous error messages
           form.parsley().reset();
       });
    
       // Handle form submission
       form.on('submit', function(event) {
          // Prevent default form submission
          event.preventDefault();
    
          // Perform Parsley validation
          form.parsley().validate();
    
          // Check if form is valid
          if (form.parsley().isValid()) {
            // Form is valid, proceed with submission 
            console.log("Form submitted successfully.");
          } else {
            // Form is invalid, display errors
            console.log("Form submission failed. Please check for errors.");
          }
       });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search