That using the stringlenth
validator you can validate the length of the string. Check the stringlength validator.
And using the date
validator you can validate the date format. Check the date validator.
If you want to validate datetime picker using validate()
you should use the logic that is to parse the value returned by the picker using SimpleDateFormat
if you are not using other date time libraries like JODA and don't care about timezone then you should create ThreadLocal
parser for datetimes.
EDIT:
validators.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
<validator name="longlength" class="jspbean.struts.vaidators.LongLengthFieldValidator"/>
</validators>
LongLengthFieldValidator.java
public class LongLengthFieldValidator extends FieldValidatorSupport {
private boolean doTrim = true;
private int maxLength = -1;
private int minLength = -1;
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public int getMaxLength() {
return maxLength;
}
public void setMinLength(int minLength) {
this.minLength = minLength;
}
public int getMinLength() {
return minLength;
}
public void setTrim(boolean trim) {
doTrim = trim;
}
public boolean getTrim() {
return doTrim;
}
public void validate(Object object) throws ValidationException {
String fieldName = getFieldName();
String val = getFieldValue(fieldName, object).toString();
if (val == null || val.length() <= 0) {
// use a required validator for these
return;
}
if (doTrim) {
val = val.trim();
if (val.length() <= 0) {
// use a required validator
return;
}
}
if ((minLength > -1) && (val.length() < minLength)) {
addFieldError(fieldName, object);
} else if ((maxLength > -1) && (val.length() > maxLength)) {
addFieldError(fieldName, object);
}
}
}
XXX-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="longlength">
<param name="fieldName">mobileno</param>
<param name="minLength">10</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>Your mobileno number needs to be 10 characters long</message>
</validator>
</validators>