2014-09-10 76 views
0

我在SmartWizard中有以下代碼部分。我想爲每個HTML控件添加自定義驗證。在SmartWizard中添加自定義驗證

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

回答

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

之前,這將有助於爲他人尋找同樣的問題,這實現來自插件的第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; 
});