File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes problem with Date validations thru Validator-rules.xml and Validation.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem with Date validations thru Validator-rules.xml and Validation.xml" Watch "problem with Date validations thru Validator-rules.xml and Validation.xml" New topic
Author

problem with Date validations thru Validator-rules.xml and Validation.xml

Ankit Pradhan
Greenhorn

Joined: Jan 05, 2005
Posts: 16
Hi
I have to make a check for the Date in a certain format(dd/MM/yyyy) on the JSP form. I am using struts framework for validation.
The problem is although i have used the validation function "date" for the date field of the form bean but still on submitt the validator is not checking for right input i.e. in the format in which i want: dd/MM/yyyy

the code for "Validation.xml" is:
<form-validation>
<formset>
<form name="/AddUpdateEmployee">
// "AddUpdateEmployee" is the name of the action mapping in Struts-config.xml
...
...
<field
property="empJoinDate"
depends="required,date">
<arg0
key="errors.date" resource="true"/>
<var>
<var-name>datePattern</var-name>
<var-value>dd/MM/yyyy</var-value>
</var>

</field>

and the code for validator-rules.xml is:
<validator name="date"
classname="org.apache.struts.util.StrutsValidator"
method="validateDate"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.date"
jsFunctionName="DateValidations">
<javascript>
<![CDATA[
function validateDate(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oDate = new employeeActionForm_DateValidations();
for (x in oDate) {
if (form[oDate[x][0]] != undefined) {
var value = form[oDate[x][0]].value;
var datePattern = oDate[x][2]("datePatternStrict");

if ((form[oDate[x][0]].type == 'text' ||
form[oDate[x][0]].type == 'textarea') &&
(value.length > 0) &&
(datePattern.length > 0)) {
var MONTH = "MM";
var DAY = "dd";
var YEAR = "yyyy";
var orderMonth = datePattern.indexOf(MONTH);
var orderDay = datePattern.indexOf(DAY);
var orderYear = datePattern.indexOf(YEAR);
if ((orderDay < orderYear && orderDay > orderMonth)) {
var iDelim1 = orderMonth + MONTH.length;
var iDelim2 = orderDay + DAY.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderDay && iDelim2 == orderYear) {
dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$");
} else if (iDelim1 == orderDay) {
dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$");
} else if (iDelim2 == orderYear) {
dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$");
} else {
dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!isValidDate(matched[2], matched[1], matched[3])) {
if (i == 0) {
focusField = form[oDate[x][0]];
}
fields[i++] = oDate[x][1];
bValid = false;
}
} else {
if (i == 0) {
focusField = form[oDate[x][0]];
}
fields[i++] = oDate[x][1];
bValid = false;
}
} else if ((orderMonth < orderYear && orderMonth > orderDay)) {
var iDelim1 = orderDay + DAY.length;
var iDelim2 = orderMonth + MONTH.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderMonth && iDelim2 == orderYear) {
dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$");
} else if (iDelim1 == orderMonth) {
dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$");
} else if (iDelim2 == orderYear) {
dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$");
} else {
dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!isValidDate(matched[1], matched[2], matched[3])) {
if (i == 0) {
focusField = form[oDate[x][0]];
}
fields[i++] = oDate[x][1];
bValid = false;
}
} else {
if (i == 0) {
focusField = form[oDate[x][0]];
}
fields[i++] = oDate[x][1];
bValid = false;
}
} else if ((orderMonth > orderYear && orderMonth < orderDay)) {
var iDelim1 = orderYear + YEAR.length;
var iDelim2 = orderMonth + MONTH.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderMonth && iDelim2 == orderDay) {
dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})$");
} else if (iDelim1 == orderMonth) {
dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})$");
} else if (iDelim2 == orderDay) {
dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})$");
} else {
dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!isValidDate(matched[3], matched[2], matched[1])) {
if (i == 0) {
focusField = form[oDate[x][0]];
}
fields[i++] = oDate[x][1];
bValid = false;
}
} else {
if (i == 0) {
focusField = form[oDate[x][0]];
}
fields[i++] = oDate[x][1];
bValid = false;
}
} else {
if (i == 0) {
focusField = form[oDate[x][0]];
}
fields[i++] = oDate[x][1];
bValid = false;
}
}
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return bValid;
}

function isValidDate(day, month, year) {
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) &&
(day == 31)) {
return false;
}
if (month == 2) {
var leap = (year % 4 == 0 &&
(year % 100 != 0 || year % 400 == 0));
if (day>29 || (day == 29 && !leap)) {
return false;
}
}
return true;
}
]]>
</javascript>
</validator>
Klaus Jesper
Greenhorn

Joined: Jan 20, 2005
Posts: 15
Just to be sure: Did you configure your action-mapping in struts-config.xml to use the validation with validate="true"?

Klaus
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Does the "required" validation work?


A good workman is known by his tools.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Oh, and it might be a good idea to use datePatternStrict.
Ankit Pradhan
Greenhorn

Joined: Jan 05, 2005
Posts: 16
hey i got the error....
actually there was a mismatch of "datePattern" and "datePatternStrict" in validation.xml and validator-rules.xml...
in validation.xml i used "datePattern" and in validator-rules.xml i used "datePatternStrict"...
now it is prompting for the correct pattern of date...
thx guys
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
Ankit, can you let me know the exact mismatch, I am getting same error
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with Date validations thru Validator-rules.xml and Validation.xml
 
Similar Threads
validation w/ javascript (date)
Struts Validator FrameWork = JavaScript Error
struts date Validation
Error while trying NewsLetter Example
future date validation