1.需要的文件
validator-rules.xml(myeclipse 生成,或者在网上下载)
validation .xml(自己添加)
ApplicationResources.properties(struts 自身的资源 文件)
包:
commons-validator.jar(validation 需要)
jakarta-oro.jar(validation 需要)
2 编写validation .xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<formset>
<constant>
<constant-name>phone</constant-name>
<constant-value>^[0-9]*$</constant-value>
</constant>
<form name="addUserActionForm">
<field property="phone" depends="mask">
<arg0 key="phone" />
<var>
<var-name>mask</var-name>
<var-value>${phone}</var-value>
</var>
</field>
<field property="tel" depends="mask">
<arg0 key="tel" />
<var>
<var-name>mask</var-name>
<var-value>${phone}</var-value>
</var>
</field>
<field property="username" depends="required">
<arg0 key="username" />
</field>
<field property="pwd" depends="required,maxlength">
<arg0 key="pwd" />
<arg1 key="${var:maxlength}" name="maxlength"
resource="false" />
<var>
<var-name>maxlength</var-name>
<var-value>7</var-value>
</var>
</field>
<field property="email" depends="email">
<arg0 key="email" />
</field>
<field property="birsday" depends="date">
<arg0 key="birsday" />
</field>
</form>
</formset>
</form-validation>
3 编写ApplicationResources.properties文件
3.0 资料文件一般有中英文对照以及默认 、ResourceBundle Editor
3.1ApplicationResources_zh.properties
errors.header=<script type="text/javascript">
errors.footer=</script>
errors.invalid={0} is invalid.
errors.er=alert("/u7528/u6237/u540D/u6216/u5BC6/u7801/u9519/u8BEF/!/u8BF7/u68C0/u67E5/u540E/u91CD/u8BD5/!");
errors.rt=alert("/u767B/u9646/u6210/u529F/!");
errors.err=alert("/u7528/u6237/u540D/u4E0D/u80FD/u4E3A/u7A7A/!");
errors.set=alert("/u4FDD/u5B58/u6210/u529F/!");
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.
#userform validation
userid=user ID
username=username
pwd=password
phone=phone
tel=telphone
birsday=birsday
email=Email
3.2ApplicationResources_zh.properties
errors.header=<script type="text/javascript">
errors.footer=</script>
errors.invalid={0} /u6709/u975E/u6CD5/u5B57/u7B26
errors.er=alert("/u7528/u6237/u540D/u6216/u5BC6/u7801/u9519/u8BEF/!/u8BF7/u68C0/u67E5/u540E/u91CD/u8BD5/!");
errors.rt=alert("/u767B/u9646/u6210/u529F/!");
errors.err=alert("/u7528/u6237/u540D/u4E0D/u80FD/u4E3A/u7A7A/!");
errors.set=alert("/u4FDD/u5B58/u6210/u529F/!");
errors.required={0} /u4E0D/u80FD/u4E3A/u7A7A
errors.minlength={0} /u957F/u5EA6/u4E0D/u80FD/u5C0F/u4E8E {1}
errors.maxlength={0} /u957F/u5EA6/u4E0D/u80FD/u5927/u4E8E {1}
errors.byte={0} /u5FC5/u987B/u4E3A/u5B57/u8282
errors.short={0} /u5FC5/u987B/u4E3A/u77ED/u6574/u6570
errors.integer={0} /u5FC5/u987B/u4E3A/u6574/u6570
errors.long={0} /u5FC5/u987B/u4E3A/u6570/u5B57
errors.float={0} /u5FC5/u987B/u4E3A/u6D6E/u70B9/u6570
errors.double={0} /u5FC5/u987B/u4E3A/u5C0F/u6570
errors.date={0} /u65E5/u671F/u683C/u5F0F/u4E0D/u6B63/u786E
errors.range={0} /u5FC5/u987B/u4F4D/u4E8E {1} /u4E0E{2}/u4E4B/u95F4
errors.creditcard={0} is an invalid credit card number.
errors.email={0} /u683C/u5F0F/u4E0D/u6B63/u786E
#userform validation
userid=/u7528/u6237ID
username=/u771F/u5B9E/u59D3/u540D
pwd=/u5BC6/u7801
phone=/u624B/u673A/u53F7/u7801
tel=/u529E/u516C/u7535/u8BDD
birsday=/u751F/u65E5
email=Email
4 在对应的XXX.jsp
添加的代码有:
<html:javascript formName="addUserActionForm" />
οnsubmit="return validateAddUserActionForm(this);"