O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.18. Validating a Multipage Form

Problem

You want to validate elements within a multipage form.

Solution

Create and invoke a validate( ) method for the custom MultiPageForm class.

Discussion

You can create a MultiPageForm.validate( ) method that leverages the Form.validate( ) method and works in much the same way. Add the following code to your Form.as file for easy inclusion in other projects:

MultiPageForm.prototype.validate = function (  ) {
  var vRes;

  // Loop through all the form pages.
  for (var i = 0; i < this.forms.length; i++) {

    // Call the validate(  ) method for each page of the form. If validate(  ) returns 
    // something other than true, some form element didn't validate. In that case,
    // display that page of the form and return the name of the offending element.
    vRes = this.forms[i].validate(  );
    if (!vRes) {
      this.setPage(i + 1);
      return vRes;
    }
  }
  return true;
};

You can use the validate( ) method for multipage forms as you did for single-page forms:

submitBtn_pb.onRelease = function (  ) {
  // Call the validate(  ) method of the multipage form.
  var valid = _root.myMultiForm.validate(  );
 
  // If the form validates, submit the form. Otherwise, display the name of the form
  // element that needs to be corrected. If an element fails to validate, the
  // multipage form automatically displays the page with the problem.
  if (valid) {
    // Submit form
  } else {
    trace(valid);
  }
};

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required