繁体   English   中英

使用parsley.js验证多页表单

[英]Validating a multi-page form with parsley.js

我正在使用这里找到的多页表单并尝试使用parsley.js验证字段集。 我在多页表单的示例页面上遵循了他们的步骤,但是遇到了以下两个问题之一:

1)在我点击提交之前,它不会提示您填写字段,然后在我点击上一个或第二个时显示错误。2)当我单击下一步时,在控制台中出现错误,指出“ next_fs.show();”

这是表单代码,下面是一个小提琴中所有当前代码的链接。

    <!-- multistep form -->
<form id="msform" data-parsley-validate>
    <!-- progressbar -->
    <ul id="progressbar">
        <li class="active">Account Setup</li>
        <li>Social Profiles</li>
        <li>Personal Details</li>
    </ul>
    <!-- fieldsets -->
    <fieldset class="first block1">
        <h2 class="fs-title">Create your account</h2>
        <h3 class="fs-subtitle">This is step 1</h3>
        <input type="text" name="email" placeholder="Email" required data-parsley-group="block1" />
        <input type="password" name="pass" placeholder="Password" required data-parsley-group="block1" />
        <input type="password" name="cpass" placeholder="Confirm Password" required data-parsley-group="block1" />
        <input type="button" name="next" class="next action-button" value="Next" data-current-block="1" data-next-block="2"  />
    </fieldset>
    <fieldset class="second block2">
        <h2 class="fs-title">Social Profiles</h2>
        <h3 class="fs-subtitle">Your presence on the social network</h3>
        ...
<input type="button" name="previous" class="previous action-button" value="Previous" data-current-block="2" data-next-block="1" />
        <input type="button" name="next" class="next action-button" value="Next" data-current-block="2" data-next-block="3" />
    </fieldset>
    <fieldset class="third block3">
        <h2 class="fs-title">Personal Details</h2>
        <h3 class="fs-subtitle">We will never sell it</h3>
        ...
<input type="button" name="previous" class="previous action-button" value="Previous" data-current-block="3" data-next-block="2" />
        <button type="submit" name="donateNow" id="donateNow" class="submit action-button" value="Submit" >Submit</button>
    </fieldset>
</form>

是我当前代码的小提琴,其中包含与parsley.js一起使用的调整。 (我目前遇到问题2)

我一辈子都无法弄清楚如何使验证在页面更改上起作用。 感谢您提供的任何帮助!

您的代码有几个问题。 关于您的第二个问题,这是由于以下原因造成的:

//this comes from the custom easing plugin
easing: 'easeInOutBack'

您将此作为animateoptions对象的一部分。 根据您的评论,这来自自定义插件,您没有将其包含在jsfiddle中。

如果注释该行,它将按预期进行动画处理( 请参阅更新的jsfiddle );

关于您的#1问题:

  1. 您的代码中未包含parsley.js
  2. 您不会将欧芹绑定到表单(应该使用$("#msform").parsley()或使用data-parsley-validate属性来绑定)
  3. 当您单击.next.prev ,始终显示下一个或上一个.prev ,而不验证该字段集/块。
  4. 您的jsfiddle与您发布的代码不匹配。 在小提琴中,您没有required属性。

您应该看一下multisteps示例 ,尤其是javascript部分,我在这里包括了附加注释:

// The form tag is <form id="demo-form" data-parsley-validate>, so 
// parsley will automatically be bound to that form.
// see the docs http://parsleyjs.org/doc/index.html#psly-usage-form
<script type="text/javascript">
$(document).ready(function () {
  // when you click on next
  $('.next').on('click', function () {
    // save current and previous blocks (these are your fieldsets)
    var current = $(this).data('currentBlock'),
    next = $(this).data('nextBlock');

    // only validate going forward. If current group is invalid, do not go further
    // .parsley().validate() returns validation result AND show errors
    if (next > current)
      // THIS IS THE IMPORTANT STEP. Validate the current block,
      // If the current block is not validated, the next block
      // won't be shown (note the return).
      if (false === $('#demo-form').parsley().validate('block' + current))
        return;

    // validation was ok. We can go on next step.
    $('.block' + current)
      .removeClass('show')
      .addClass('hidden');

    $('.block' + next)
      .removeClass('hidden')
      .addClass('show');

  });
});
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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