struts
package mons
import com
import com
public class CardIdFieldValidator extends FieldValidatorSupport{
public void validate(Object object) throws ValidationException
{
// 獲得字段的名字
String fieldName = getFieldName();
// 獲得輸入界面輸入的值
String value = getFieldValue(fieldName
if (value == null || value
return;
if(value
addFieldError(fieldName
if(value
validate
if(value
validate
}
/** *//**
* <p>
*
* 根據〖中華人民共和國國家標准 GB
* 排列順序從左至右依次為
*
* 第十八位數字(校驗碼)的計算方法為
*
*
*
*
*
*
* @date Oct
* @param value
* @param object
*/
public void validate
{
// 獲得字段的名字
String fieldName = getFieldName();
String tempStr = value
String sourceCheckCode = value
String checkCode =
int[] a = new int[
int i =
try
{
while(i<
a[i] = Integer
i++;
}
} catch (NumberFormatException e)
{
addFieldError(fieldName
}
int mod = (a[
+a[
switch (mod){
case
checkCode =
break;
case
checkCode =
break;
case
checkCode =
break;
case
checkCode =
break;
case
checkCode =
break;
case
checkCode =
break;
case
checkCode =
break;
case
checkCode =
break;
case
: checkCode =
break;
case
checkCode =
break;
case
checkCode =
break;
}
if(!sourceCheckCode
addFieldError(fieldName
}
/** *//**
* <p>
*
* 只做了數字驗證
* @date Oct
* @param value
* @param object
*/
public void validate
{
String fieldName = getFieldName();
int i =
try
{
while(i<
if(!Character
addFieldError(fieldName
i++;
}
} catch (NumberFormatException e)
{
addFieldError(fieldName
}
}
}
<validator name=
<field name=
<field
<message>身份證號不能為空
</field
<field
<message>身份證號格式不正確
</field
</field>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28891.html