如何使用parsley.js显示/隐藏字段验证div

时间:2014-12-28 21:16:40

标签: javascript jquery parsley.js

所以我想上下文,这是一个来自parsley.js文档的例子。

<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>

让我们说我有一个名为&#34; checkmark&#34;的隐藏div。如何在验证firstname字段时显示此div?

我还应该澄清一下,我已经阅读了文档,但仍然不了解如何完成我在这里尝试做的事情,所以发布文档的链接并不会真正有用除非你在答案中使用它。

1 个答案:

答案 0 :(得分:1)

您应该使用Parsley's Events。由于您希望根据字段验证显示或隐藏某些内容,因此您应使用parsley:field:successparsley:field:error

工作示例(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>

这就是我的所作所为:

  1. ìd=checkmark字段后添加了firstname的div(隐藏类,因为您使用的是Bootstrap)。
  2. 添加了这个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');
        }
    });
    

    此代码将侦听Parsley验证的每个输入的验证。这意味着当字段lastname失败时,$.listen('parsley:field:error', function(ParsleyField) {内的代码将被执行。这就是为什么我使用if来检查attr名称是否为firstname

    然后根据验证结果显示或隐藏div。

  3. 在字段中添加了required,因此当您点击按钮时会显示该消息。