01 November 2016

Struts 2 Framework: Validator Annotation

Regex cPassword validator:

@ExpressionValidator(message = "Please confirm password",key = "confirm.password",
shortCircuit = true, expression ="password.equals(confirmPassword)" )
Regex password validator: 

@RequiredStringValidator(key = "password.required")
@RegexFieldValidator(key = "password.rules",regexExpression = "^(?=.*[0-9])(?=.*[A-Z]).{6,}$")
public void setPassword(String password) {
    this.password = password;
}
Regex phone validator: 

@RequiredStringValidator(message="Phone Number is required")
@RegexFieldValidator(message="Invalid Phone Number",expression = "\\d{3}-\\d{3}-\\d{4}")
public void setPhone(String phone) { 
    this.phone= phone; 
}
Required & email validator: 

@RequiredStringValidator(message = "Please enter your e-mail address.")
@EmailValidator(message = "Please enter a valid e-mail address.")
public void setEmail(String email) {
    this.email = email;
}
Required password validator: 

@RequiredStringValidator(message = "Please enter your password.")
public void setPassword(String password) {
    this.password = password;
}
Double-validator: 

@DoubleRangeFieldValidator(
        minInclusive = "23.5", maxInclusive = "57.8",
        message = "The scale must bet between ${minInclusive} and ${maxInclusive} (inclusive)"
        )
public void setScale(double scale) {
    this.scale = scale;
}
Date-validator: 

@DateRangeFieldValidator(
        min = "1/1/60",
        max = "12/31/95",
        message = "Please enter your birthday between ${min} and ${max}"
        )
public void setBirthday(Date birthday) {
    this.birthday = birthday;
}
Integer-validator: 

@IntRangeFieldValidator(min = "18", max = "25",
        message = "Your age must be between ${min} and ${max}")
public void setMyAge(int myAge) {
    this.myAge = myAge;
}
String-validator:

@StringLengthFieldValidator(
        minLength = "6",
        maxLength = "12",
        message = "Your password must have from ${minLength} to ${maxLength} characters"
        )
public void setPassword(String password) {
    this.password = password;
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang