Struts2 Custom Validator (identity card verification)

Source: Internet
Author: User
Tags object object root directory

Struts2 Validator is the xwork inside of the validation, custom validator is based on the source code inherits existing field validator. The specific steps are as follows:

1. Expand the Xwork-2.0.4.jar,com.opensymphony.xwork2.validator.validators directory to have a default.xml, copy it to the project root directory renamed Validators.xml.

The validation framework first finds the Validators.xml file in the root directory, the Validators.xml file is not found, and the validation framework invokes the default validation settings, that is, the configuration information inside the default.xml.

4. Create a new class (ID card Verification For example: class name Cardidfieldvalidator) inherit Fieldvalidatorsupport, implement Validate method.

Package org.siyn.commons.validators;
Import com.opensymphony.xwork2.validator.ValidationException;
Import Com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class Cardidfieldvalidator extends Fieldvalidatorsupport
{
public void Validate (Object object) throws Validationexception
{
Get the name of the field
String fieldName = GetFieldName ();
Get the value entered in the input interface
String value = GetFieldValue (FieldName, Object). toString ();
if (value = null | | value.length () <= 0)
Return
if (Value.length ()!=15 && value.length ()!=18)//ID must be 15 or 18 bits!
Addfielderror (FieldName, object);
if (Value.length () ==15)
Validate15cardid (Value, object);
if (Value.length () ==18)
Validate18cardid (Value, object);
}
/** *//**
* <p>18 ID Card Verification </p>

*

* According to the national standard GB 11643-1999〗, the citizenship number is the characteristic combination code, which consists of 17 digit ontology code and one digit check code.

* Order from left to right: six-digit address code, eight-digit birth date code, three-digit sequence code and a digital parity code.

*

* The 18th digit (parity code) calculation method is:

* 1. The previous ID number 17 digits are multiplied by different coefficients respectively. The coefficients from the first to the 17th position are: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

* 2. Add the results by multiplying the 17-digit number and the coefficient.

* 3. Add out and divide by 11 to see what the remainder is?

* 4. The remaining number can only have 0 1 2 3 4 5 6 7 8 9 10 of these 11 digits. The number of the last ID card corresponding to the respective is 1 0 X 9 8 7 6 5 4 3 2.

* 5. Through the above learned that if the remainder is 2, it will appear in the ID number 18th digits of the Roman numeral Ⅹ. If the remainder is 10, the last number of the ID card is 2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.