4

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 confirm password fields.

Below is my code:

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 the same data?

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • You can use Struts2-JSR303 plugin which provides this type of validation out of the box for you.https://github.com/umeshawasthi/jsr303-validator-plugin – Umesh Awasthi Dec 30 '13 at 07:32
  • @UmeshAwasthi I did validation by using Hibernate. If I use struts2 there we can directly put condition in RequiredFielsdValidator annotation. like that here is it possible? –  Dec 30 '13 at 07:41
  • I believe you are using Hibernate Validator? and you can use that with this plugin.this is how you can do that https://github.com/umeshawasthi/struts2-jsr303-example/blob/master/src/main/java/org/demo/validation/actions/dto/CrossFieldConstraintsDTO.java – Umesh Awasthi Dec 30 '13 at 07:46
  • @UmeshAwasthi I downloaded but I can't able to add this plugin to my Netbeans IDE, Could u please give me the exact URL thank u very much –  Dec 30 '13 at 08:02
  • how you installing it ? are you using maven or something similar or you can just download jar from following location http://central.maven.org/maven2/com/github/umeshawasthi/struts2-jsr303-validation-plugin/1.0/ – Umesh Awasthi Dec 30 '13 at 08:05
  • @UmeshAwasthi I am not using MAVEN. –  Dec 30 '13 at 09:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44143/discussion-between-codegeek-and-umesh-awasthi) –  Dec 30 '13 at 09:22

3 Answers3

3

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 implement validate method which is inherited from Validator interface but doesn't have default implementation. You should write this implementation to do custom validation. For example you want to create a RetypeValidator which validates two fields have the same value. It could look like

public class RetypeValidator extends ValidatorSupport {

  private String value = null;

  public String getValue() {
    return value;
  }
  public void setValue(String value) {
    this.value = value;
  }

  private String retypeValue = null;

  public String getRetypeValue() {
    return retypeValue;
  }

  public void setRetypeValue(String value) {
    retypeValue = value;
  }

  @Override
  public void validate(Object object) throws ValidationException {
    String value = (String) parse(this.value, String.class);
    String retypeValue = (String) parse(this.retypeValue, String.class);
    if (value != null && retypeValue != null && !value.equals(retypeValue))
      addActionError(getDefaultMessage());
  }
}

then you have to add this validator to the configuration in the validators.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Config 1.0//EN"
        "http://struts.apache.org/dtds/xwork-validator-config-1.0.dtd">

<validators>
  <validator name="retypeValidator" class="org.custom.struts.vaidators.RetypeValidator"/>
</validators>

Now, you have a custom validator name that you could use with @CustomValidator annotation.

@Validations(
    customValidators = @CustomValidator(type="retypeValidator", message="the value and retype value should be the same",
      parameters = { @ValidationParameter( name = "value", value = "${password}" ), @ValidationParameter( name = "retypeValue", value = "${conformPassword}" )})
)

Note, password and conformPassword are OGNL expressions used to parse when being validated.

Roman C
  • 49,761
  • 33
  • 66
  • 176
1

You can use if statement to compare

if(password == conformPassword)
{
    //TO-DO
}
else
{
    //TO-DO
}
AhmetEmre90
  • 501
  • 2
  • 8
  • 23
  • I want to compare two fields in within annotation .thanking you for your quick response –  Dec 30 '13 at 07:49
  • This code doesn't answer a question and should be either comment or deleted altogether. – Roman C Jan 03 '14 at 09:44
0
if(getPassword().equals(getConformPassword()){
{
//code
}
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
  • thank u Manish I want to compare two fields in within annotation .thanking you for your quick response –  Dec 30 '13 at 07:49
  • This code doesn't answer a question and should be either comment or deleted altogether. – Roman C Jan 03 '14 at 09:44