在SmartWizard中添加自定义验证

时间:2014-09-10 08:28:11

标签: jquery validation smart-wizard

我在智能向导中有以下代码部分。我想为每个HTML控件添加自定义验证。

  <form id="myform">
    <input type="text" name="field1" />
    <br/>
    <input type="text" name="field2" />
    <br/>
    <input type="submit" />
</form>  

我尝试了下面的代码但没有工作。

function leaveAStepCallback(obj){
               var step_num= obj.attr('rel'); // get the current step number
        return validateSteps(step_num); // return false to stay on step and true to continue navigation 
      }

2 个答案:

答案 0 :(得分:3)

我知道这已经过时但我还是会回答,因为我在自己做之前发现了这篇文章

//..init wizard
onLeaveStep: function(obj, context){ 
    //Validate the current step                                  
    var frst = "#step-"+context.fromStep;
    var container = this.elmStepContainer.find(frst);
    //container now contains everything in the box
    //you can now container.find("...") to get fields
    //and then run some regex or whatever you want to do the validation
    if(invalid_fields.length > 0){
        return false ;
    }else{ return true;}},
//...the rest of the wizard init

答案 1 :(得分:1)

这对于寻找相同问题的其他人有帮助,这个实现来自插件的第4版。对此事件方法返回false将停止步骤的传播,因此保持错误的同一步骤。

$("#smartwizard").on("leaveStep", function(e, anchorObject, stepNumber) {
    var formElms = $(anchorObject.attr('href')).find("input, textarea");
    var hasError = false;
    $.each(formElms, function(i, elm){
        if(elm.val().length == 0){
           hasError = true;
        }
    });
    return !hasError;
});
相关问题