validation电话号吗,mask

<form-validation> 
  <formset> 
    <constant>//全局常量定义 
      <constant-name>phone</constant-name>//电话号码 
      <constant-value>^\(?(\d{3});\);?[-| ]?(\d{3});[-| ]?(\d{4});$</constant-value> 
    </constant> 
    <form name="loginForm"> //form名称

      <field property="username" depends="required,maxlength,minlength,mask"> //property名称
	<msg name="required" key="username.required"/> //对应properties文件的key(页面显示的错误提示);
	<msg name="maxlength" key="username.maxlength"/> 
	<msg name="minlength" key="username.minlength"/> 
	<msg name="mask" key="username.legal"/> 
	<arg0 name="maxlength" key="${var:maxlength}" resource="false" /> //设置参数
	<arg1 name="minlength" key="${var:minlength}" resource="false" /> 
	<arg2 name="mask" key="${var:mask}" resource="false" /> 
	<var> //变量
	  <var-name>maxlength</var-name> 
	  <var-value>16</var-value> 
	</var> 
	<var> 
	  <var-name>minlength</var-name> 
	  <var-value>6</var-value> 
	</var> 
	<var> 
	  <var-name>mask</var-name> //用户名的规定的字符内
	  <var-value>^[_a-z0-9-]+(._a-z0-9-]+);*a-z0-9-]+(.a-z0-9-]+);*$</var-value> 
	</var> 
      </field> 

      <field property="psw1" depends="required,minlength,twofields"> //两个密码是否一致的检查
	<msg name="required" key="psw1.required"/> 
	<msg name="minlength" key="psw1.minlength"/> 
	<msg name="twofields" key="errors.twofields1"/>//这个是自己定义的~~,在validator-rules.xml中有添加的~~ 
	<arg0 name="minlength" key="${var:minlength}" resource="false" /> 
	<var> 
	  <var-name>minlength</var-name> 
	  <var-value>6</var-value> 
	</var> 
	<var> 
	  <var-name>secondProperty</var-name> 
	  <var-value>confirm1</var-value> 
	</var> 
      </field>
      
      <field property="email" depends="email"> 
	<msg name="email" key="error.email"/> //EMAIL
      </field> 

      <field property="teleph" depends="mask"> //电话号吗
	<msg name="mask" key="error.teleph"/> 
	<arg0 name="mask" key="${var:mask}" resource="false" /> 
	<var> 
		<var-name>mask</var-name> 
		<var-value>${phone}</var-value> 
	</var> 
      </field> 

      <field property="day" depends="range"> //指定范围
	<asg name ="range" key="error.day" /> 
	<arg1 name="range" key="${var:min}" resource="false" /> 
	<arg2 name="range" key="${var:max}" resource="false" /> 
	<var> 
		<var-name>min</var-name> 
		<var-value>1</var-value> 
	</var> 
	<var> 
		<var-name>max</var-name> 
		<var-value>31</var-value> 
	</var> 
      </field> 

    </form> 
  </formset> 
</form-validation> 


以上是validation.xml中的验证.
其中twofields是自己定义了,先写一个类:
package web.common;

import org.apache.commons.validator.ValidatorUtil;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.Resources;
import javax.servlet.http.HttpServletRequest;

public class VaConsistent {
    public static boolean validateTwoFields( Object bean, ValidatorAction va,
                                             Field field, ActionErrors errors,
                                            HttpServletRequest request); {
        String value = ValidatorUtil.getValueAsString(bean, field.getProperty(););;
        String sProperty2 = field.getVarValue("secondProperty");;
        String value2 = ValidatorUtil.getValueAsString(bean, sProperty2);;
        if (!GenericValidator.isBlankOrNull(value);); {
            try {
                if (!value.equals(value2);); {
                  errors.add(field.getKey();,Resources.getActionError(request, va, field););;
                    return false;
                }
            }
            catch (Exception e); {
                errors.add(field.getKey();,Resources.getActionError(request, va, field););;
                return false;
            }
        }
        return true;
    }
}


   在文件validator-rules.xml中<global>之间加上:
     <validator name="twofields" classname="web.common.VaConsistent" method="validateTwoFields" methodParams="java.lang.Object,
	org.apache.commons.validator.ValidatorAction,
	org.apache.commons.validator.Field,
	org.apache.struts.action.ActionErrors,
	javax.servlet.http.HttpServletRequest"
	depends="required" msg="errors.twofields">

    <javascript><![CDATA[
        function validateTwoFields(form); {
            var bValid = true;
            var focusField = null;
            var i = 0;
            var fields = new Array();;
            oTwoFields = new twofields();;
            for (x in oTwoFields); {
                var field = form[oTwoFields[x][0]];
                var secondField = form[oTwoFields[x][2]("secondProperty");];
                if (field.type == 'text' ||
                    field.type == 'textarea' ||
                    field.type == 'select-one' ||
                    field.type == 'radio' ||
                    field.type == 'password'); {
                    var value;
                    var secondValue;
                    // get field's value
                    if (field.type == "select-one"); {
                        var si = field.selectedIndex;
                        value = field.options[si].value;
                        secondValue = secondField.options[si].value;
                    } else {
                        value = field.value;
                        secondValue = secondField.value;
                    }
                    if (value != secondValue); {
                        if (i == 0); {
                      focusField = field;
                        }
                        fields[i++] = oTwoFields[x][1];
                        bValid = false;
                    }
                }
            }
            if (fields.length > 0); {
                focusField.focus();;
                alert(fields.join('\n'););;
            }
            return bValid;
        }]]></javascript>
      </validator>


这样就OK了.

tracy_katrina束于2005-1-18 22:01

你可能感兴趣的:(apache,bean,xml,servlet,struts)