在SmartWizard中添加自定义验证
问题描述:
我在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
}
答
我知道这是非常过时的,但我要去反正回答,因为我发现这个职位做自己
//..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
答
之前,这将有助于为他人寻找同样的问题,这实现来自插件的第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;
});