I am doing web application in that I want to validate two fields in the JSP form. In registration filed I have so many fields.In that I want to validate password and conform password fields.
Below is my code:
Validation code in Struts2 Action Class:
@Length(min = 6, max = 20)
@Column(name = "PERSON_PASSWORD", nullable = false, length = 20)
public String getPassword() {
return password;
}
@Length(min = 6, max = 20)
@Column(name = "PERSON_CONFORMPASSWORD", nullable = false, length = 20)
public String getConformPassword() {
return conformPassword;
}
Now how can I validate the two fields contain same data are not?
You can use if statement to compare
You could use a non-field custom validator to validate any number of fields. For this purpose you should create a custom validator that extend
ValidatorSupport
and implementvalidate
method which is inherited fromValidator
interface but doesn't have default implementation. You should write this implementation to do custom validation. For example you want to create aRetypeValidator
which validates two fields have the same value. It could look likethen you have to add this validator to the configuration in the
validators.xml
:Now, you have a custom validator name that you could use with
@CustomValidator
annotation.Note,
password
andconformPassword
are OGNL expressions used to parse when being validated.