Code Listing 1: Sample validator-rules.xml configuration file

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons
           Validator Rules Configuration 1.0//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">

<form-validation>
  <global>
    <validator name="required"
          classname="org.apache.struts.validator.FieldChecks"
             method="validateRequired"
       methodParams="java.lang.Object,
                     org.apache.commons.validator.ValidatorAction,
                     org.apache.commons.validator.Field,
                     org.apache.struts.action.ActionErrors,
                     javax.servlet.http.HttpServletRequest"
                msg="errors.required">
      <javascript>
        <![CDATA[
        function validateRequired(form) {
          var isValid = true;
          var focusField = null;
          var i = 0;
          var fields = new Array();
          oRequired = new required();
          for (x in oRequired) {
            var field = form[oRequired[x][0]];

            if (field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'file' ||
                field.type == 'select-one' ||
                field.type == 'radio' ||
                field.type == 'password') {

              var value = '';

              // get field's value
              if (field.type == "select-one") {
                var si = field.selectedIndex;
                if (si >= 0) {
                  value = field.options[si].value;
                }
              } else {
                value = field.value;
              }

              if (trim(value).length == 0) { {
                if (i == 0) {
                  focusField = field;
                }
                fields[i++] = oRequired[x][1];
                isValid = false;
              }
            }
          }

          if (fields.length > 0) {
            focusField.focus();
            alert(fields.join('\n'));
          }

          return isValid;
        }

        // Trim whitespace from left and right sides of s.
        function trim(s) {
          return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
        }

        ]]>
      </javascript>
    </validator>
  </global>
</form-validation>
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy