1

I'm using a jquery smart wizard script to make my forms into wizards.

This works very well, but the validation code looks like it could be made simpler.

This is the example they provide. Could this be made into a loop so I don't have to write it for each of my 5 steps ?

function validateSteps(step){
  var isStepValid = true;
   if(step == 1){
    if(validateStep1() == false ){
      isStepValid = false; 
      $('#wizard').smartWizard('showMessage','Please correct the errors in step '+step+ ' and click next.');
      $('#wizard').smartWizard('setError',{stepnum:step,iserror:true});         
    }else{
      $('#wizard').smartWizard('hideMessage');
      $('#wizard').smartWizard('setError',{stepnum:step,iserror:false});
    }
}

    if(step == 3){
     if(validateStep3() == false ){
       isStepValid = false; 
       $('#wizard').smartWizard('showMessage','Please correct the errors in step '+step+ ' and click next.');
       $('#wizard').smartWizard('setError',{stepnum:step,iserror:true});         
     }else{
       $('#wizard').smartWizard('hideMessage');
       $('#wizard').smartWizard('setError',{stepnum:step,iserror:false});
     }
    }

    return isStepValid;
 }

Thanks

4
  • 1
    Well you need to post these kinda questions here Commented May 13, 2015 at 9:19
  • For optimizing you need to show how validateStep1() and validateStep2() , ..... so on, looks like! so can generalize it. Commented May 13, 2015 at 9:21
  • 3
    @GuruprasadRao Sad to say it but, "This is the example they provide." indicates that the OP did not write this code himself which makes it off-topic for Code Review. Commented May 13, 2015 at 9:31
  • I agree @SimonAndréForsberg. Well he can remove it though!! :) Commented May 13, 2015 at 9:32

1 Answer 1

1

You could do this,

function validateSteps(){
        var isStepValid = true, step =1;
        for(step ;step <=5;step ++){
            if(window["validateStep"+step ]() == false ){
              isStepValid = false; 
              $('#wizard').smartWizard('showMessage','Please correct the errors in step '+step + ' and click next.');
              $('#wizard').smartWizard('setError',{stepnum:step ,iserror:true});         
            }else{
              $('#wizard').smartWizard('hideMessage');
              $('#wizard').smartWizard('setError',{stepnum:step ,iserror:false});
            }

        }
        return isStepValid;

    }

You just have to worry about having a function named validateStepX

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that seems to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.