简体   繁体   中英

How do I show/hide a div on field validation with parsley.js

So I guess for context, here is an example from the parsley.js documentation.

<form id="demo-form" data-parsley-validate>
  <div class="first">
    <label for="firstname">Firstname:</label>
    <input type="text" name="firstname" data-parsley-range="[4, 20]" data-parsley-group="block1" />

    <label for="lastname">Lastname:</label>
    <input type="text" name="lastname" data-parsley-range="[4, 20]" data-parsley-group="block1" />
  </div>
  <hr></hr>
  <div class="second">
    <label for="fullname">Fullname:</label>
    <input type="text" name="fullname" data-parsley-range="[8, 40]" data-parsley-group="block2" />
  </div>

  <div class="invalid-form-error-message"></div>
  <input type="submit" class="btn btn-default validate" />
</form>

<script type="text/javascript">
$(document).ready(function () {
  $('#demo-form').parsley().subscribe('parsley:form:validate', function (formInstance) {

    // if one of these blocks is not failing do not prevent submission
    // we use here group validation with option force (validate even non required fields)
    if (formInstance.isValid('block1', true) || formInstance.isValid('block2', true)) {
      $('.invalid-form-error-message').html('');
      return;
    }
    // else stop form submission
    formInstance.submitEvent.preventDefault();

    // and display a gentle message
    $('.invalid-form-error-message')
      .html("You must correctly fill the fields of at least one of these two blocks!")
      .addClass("filled");
    return;
  });
});
</script>

Let's say I have a hidden div called "checkmark". How would I show this div upon validation of the firstname field?

I should also clarify that I have read the documentation, but still don't understand how to accomplish what I'm trying to do here, so posting a link to the documentation is not really going to be helpful unless you are using it in your answer.

You should use Parsley's Events . Since you want to display or hide something based on a field validation, you should use parsley:field:success and parsley:field:error .

Working example ( with jsfiddle ):

<form id="demo-form" data-parsley-validate>
    <div class="first">
        <label for="firstname">Firstname:</label>
        <input type="text" name="firstname" data-parsley-range="[4, 20]" data-parsley-group="block1" required />
        <div class="hidden" id="checkmark">This message will be shown when the firstname is not valid </div>

        <label for="lastname">Lastname:</label>
        <input type="text" name="lastname" data-parsley-range="[4, 20]" data-parsley-group="block1" />
    </div>
    <hr></hr>
    <div class="second">
        <label for="fullname">Fullname:</label>
        <input type="text" name="fullname" data-parsley-range="[8, 40]" data-parsley-group="block2" />
    </div>

    <div class="invalid-form-error-message"></div>
    <input type="submit" class="btn btn-default validate" />
</form>
<script>
$(document).ready(function () {
    $('#demo-form').parsley().subscribe('parsley:form:validate', function (formInstance) {        
        // if one of these blocks is not failing do not prevent submission
        // we use here group validation with option force (validate even non required fields)
        if (formInstance.isValid('block1', true) || formInstance.isValid('block2', true)) {
            $('.invalid-form-error-message').html('');
            return;
        }
        // else stop form submission
        formInstance.submitEvent.preventDefault();

        // and display a gentle message
        $('.invalid-form-error-message')
        .html("You must correctly fill the fields of at least one of these two blocks!")
        .addClass("filled");
        return;
    });

    $.listen('parsley:field:error', function(ParsleyField) {
        if(ParsleyField.$element.attr('name') === 'firstname') {
            $("div#checkmark").addClass('show').removeClass('hidden');
        }
    });

    $.listen('parsley:field:success', function(ParsleyField) {
        if(ParsleyField.$element.attr('name') === 'firstname') {
            $("div#checkmark").addClass('hidden').removeClass('show');
        }
    });
});
</script>

And here's what I did:

  1. Added a div with ìd=checkmark after the firstname field (with a hidden class, since you're using Bootstrap).
  2. Added this block of javascript:

     $.listen('parsley:field:error', function(ParsleyField) { if(ParsleyField.$element.attr('name') === 'firstname') { $("div#checkmark").addClass('show').removeClass('hidden'); } }); $.listen('parsley:field:success', function(ParsleyField) { if(ParsleyField.$element.attr('name') === 'firstname') { $("div#checkmark").addClass('hidden').removeClass('show'); } }); 

    This code will listen to the validation of every input validated by Parsley. This means that when the field lastname fails, the code inside $.listen('parsley:field:error', function(ParsleyField) { will be executed. This is why I used an if to check if the attr name is firstname .

    Then you show or hide the div based on the validation result.

  3. Added required to the field, so the message is displayed when you click on the button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM