Hi just put the <validator....></validator> into the validator-rules.xml
of the Struts Validation Framework. And create a Java class contain the
validation method as I posted before. If you want to use it in your
code, do something like this:
<field property="startTime" depends="time">
<arg0 key="your.message.key"/>
</field>
Good luck
Pankaj Gupta wrote:
>
> Hi,
>
> I tried using your code but its not working. Can you please tell me
> what do I need to write in validation.xml.
>
> I am writing this:
> <field property="startTime"
> depends="time">
> <arg0 key=""/>
> </field>
>
> Truong Xuan Tinh wrote:
>> Hi there, here is my implementation on the client side, it's validate
>> time against the pattern HH:mm, you can customize against HH:mm:ss with
>> ease:
>> <validator name="time"
>> classname="MyValidatorClass"
>> method="validateTime"
>> methodParams="java.lang.Object,
>> org.apache.commons.validator.ValidatorAction,
>> org.apache.commons.validator.Field,
>> org.apache.struts.action.ActionMessages,
>> org.apache.commons.validator.Validator,
>> javax.servlet.http.HttpServletRequest"
>> msg="errors.time"
>> jsFunctionName="TimeValidations">
>> <javascript>
>> <![CDATA[
>> function validateTime(form){
>> var isValid = true;
>> var focusField = null;
>> var i = 0;
>> var fields = new Array();
>> var formName = form.getAttributeNode("name");
>>
>> oTime = eval('new ' + formName.value + '_TimeValidations()');
>> for (x in oTime) {
>> var field = form[oTime[x][0]];
>> var value = field.value;
>> if ((field.disabled == false) && (value.length > 0) &&
>> (field.type == 'text' || field.type == 'textarea')) {
>> var timeParts = field.value.split(":");
>> var hour = timeParts[0];
>> var minute = timeParts[1];
>> try{
>> if(hour.length != 2 || minute.length != 2){
>> fields[i++] = oTime[x][1];
>> focusField = field;
>> isValid = false;
>> } else {
>> if(isNaN(hour)||isNaN(minute)){
>> fields[i++] = oTime[x][1];
>> focusField = field;
>> isValid = false;
>> } else {
>> hour = parseInt(hour);
>> minute = parseInt(minute);
>> if(hour>=24 || hour < 0 || minute >=60 || minute <0){
>> fields[i++] = oTime[x][1];
>> focusField = field;
>> isValid = false;
>> }
>> }
>> }
>> } catch(e){
>> fields[i++] = oTime[x][1];
>> focusField = field;
>> isValid = false;
>> }
>> }
>> }
>> if (fields.length > 0) {
>> focusField.focus();
>> alert(fields.join('\n'));
>> }
>> return isValid;
>> }
>> ]]>
>> </javascript>
>> </validator>
>> Here is the Server side: this method is in the MyValidatorClass
>> public static boolean validateTime(Object bean, ValidatorAction va,
>> Field field,
>> ActionMessages errors, Validator validator,
>> HttpServletRequest request) {
>> boolean result = true;
>> try {
>> String value = ValidatorUtils.getValueAsString(bean,
>> field.getProperty());
>> if (!GenericValidator.isBlankOrNull(value)) {
>> result = Pattern.matches("[0-2][\\d]:[0-5][\\d]",
>> value);
>> if (result == true) {
>> String hour = value.substring(0, 2);
>> String minute = value.substring(3);
>> int hourInt = Integer.parseInt(hour);
>> int minuteInt = Integer.parseInt(minute);
>> if (hourInt < 0 || hourInt >= 24 || minuteInt < 0 ||
>> minuteInt >= 60) {
>> result = false;
>> }
>> }
>> }
>> } catch (NumberFormatException e) {
>> result = false;
>> e.printStackTrace();
>> }
>> if (result == false) {
>> errors.add(field.getKey(),
>> Resources.getActionMessage(validator, request, va, field));
>> }
>> return result;
>> }
>> Maybe it's not the best you can found on the web! Use it at your own
>> risk.
>> Pankaj Gupta wrote:
>>
>>> Is there a validation-rule for time(hh:mm:ss format) as it exists for
>>> date in struts validator framework? If not, has anyone implemented it?
>>> I dont want to reinvent the wheel.
>>>
>>> regards,
>>> Pankaj
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>>> For additional commands, e-mail: [EMAIL PROTECTED]
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]