Expand my Community achievements bar.

formattedValue for time

Avatar

Former Community Member

Hello all,

I'm having some trouble with the formattedValue property.

So far, i have used it successfully in order to validate the date format when the user exits a date field in my form. The solution was quite easy to find, and although i didn't understand fully how it worked it copied/pasted it in my javascript code.

For those interested, here's the deal:

if(this.rawValue == this.formattedValue){
    xfa.host.messageBox("Please use the calendar or enter a date in the format DD/MM/YYYY (e.g. 05/03/2011)");
    xfa.host.setFocus(this.somExpression);
    this.rawValue = "";
}

As i understood it, when the user inputs something in a date field the "formattedValue" of the field is the input formatted with the format defined in the object property, eg DD/MM/YYYY in my case. If it cannot be formatted, then the "formattedValue" is equal to the "rawValue" and thus there is some kind of problem that is handled by the "if" condition.

I also have time fields in my form. In the object properties, i have set the format to "HH:MM:SS".

Naturally, i thought I could use the same trick to force the user to input a correct time format. But there's some difficulties: it works except when the user actually inputs a time that is already correctly formatted. So, if the user inputs "12:05:05" for example, the formattedValue is equal to the rawValue. Thus the if condition is true and that should not be the case...

of course, i could write a few lines to split the string, parse into int, then check the values (>0 and <24 for hours, etc.), but i'd like to have something a little bit cleaner...

With the date, everything works fine, because the rawValue, for some reason, is automatically formatted with YYYY-MM-DD. Thus the formattedValue is never the same as the rawValue when the date is correct...

Isn't there a way to find a simple solution? maybe have a time default format like "hh:mm:ss:ms" (with milliseconds) ?

thank you.

11 Replies

Avatar

Level 10

You can do this in this way.

if (this.formattedValue === this.rawValue)

{

xfa.host.messageBox("Wrong Date Format");

xfa.host.setFocus(this);

this.rawValue = "";

}

You also can define a validation pattern in your date field, then you don't need a script.

Avatar

Former Community Member

I will try this solution, thanks.

In the meantime, i got around the problem by defining the display pattern with "HH:MM:SS " (with a blank space at the end). I figured that the property "formattedValue" actually gives the "display pattern". This kind of information was impossible to find in the adobe documentation, but it did the trick for me...

about the validation pattern, i tried it too. But it can only display a warning box. i wanted to set the focus on my field and erase the input. If there's a way to do it with the validation pattern, this is even a simplier solution.

Thanks, anyway, radzmar, i've seen your nickname quite a lot on the forum, you seem to know quite a lot about adobe!

Avatar

Level 10

Hi again,

here some Edit Pattern that allow a more flexible input of a date/time for users.

Many users hate to use : as separator, as it is not close to the number block on the keyboard, so with the edit pattern you can define other separators like /-, which then can be used for the input too.

Edit Pattern:

date{DD.MM.YYYY}|date{DDMMYYYY}|date{DD MM YYYY}|date{DD-MM-YYYY}|date{DD:MM:YY}|date{DDMMYY}|date{DD MM YY}|date{DD-MM-YY}|date{DD,MM,YYYY}|date{DD/MM/YYYY}|date{DD,MM,YY}|date{DD/MM/YY}

Display Pattern:

date{DD.MM.YYYY}

Input > Formatted Value:

311211 > 31:12:2011

01 01 11 > 01:01:2011

12-08-14 > 12:08:2014

22.11.2020 > 22:11:2020

14/12/10 > 14:12:2010

22,11,00 > 22:11:2000

Edit Pattern:

time{HH:MM:SS}|time{HHMMSS}|time{HH MM SS}|time{HH-MM-SS}|time{HH:MM}|time{HHMM}|time{HH MM}|time{HH-MM}|time{HH.MM.SS}|time{HH.MM}|time{HH,MM}|time{HH,MM,SS}|time{HH/MM}|time{HH/MM/SS}

Display Pattern:

time{HH:MM:SS}

Input > Formatted Value:

00 > 00:00:00

123456 > 12:34:56

01-33 > 01:33:00

14.55 > 14:55:00

09 08 07 > 09:08:07

23:23 > 23:23:00

14/14 > 14:14:00

12,45 > 12:45:00

Avatar

Level 1

I have used your javascript but the strange thing is that if I save the form and later start again all the times are gone!!!

What is wrong?

sof new.JPG

Oscar

Avatar

Former Community Member

Radzmar,

I have used your javascript and it works for me as well.  The only issue I have is when you enter 2400. It will not formated to 24:00 or 12:00. Any suggestion?

Thank you,

Avatar

Level 10

To display times from 1-24 your have to change the hours symbol in the pattern from HH to KK.

Display Pattern:

time{KK:MM:SS}

Edit Pattern:

time{KK:MM}|time{KKMM}|time{KK MM}|time{KK-MM}|time{KK.MM.SS}|time{KK.MM}|time{KK,MM}

Avatar

Former Community Member

I am working on timesheet form

I have Start and End time fields:

 

Date/Time field

Pattern is set this way

Display: time{KK:MM}

Edit : time{KK:MM}|time{KKMM}|time{KK MM}|time{KK-MM}|time{KK.MM.SS}|time{KK.MM}|time{KK,MM}

Data and Validate tabe are empty:

This is the code on Exit event:

if

(this.formattedValue === this.rawValue)

{

xfa.host.messageBox("Please enter correct data format");

xfa.host.setFocus(

this);

this.rawValue

= "";

}

This is the code on Change event:

var

maxLength = 4;

if

(xfa.event.newText.length > maxLength )xfa.event.change = "";

 

 

On validate event I have a code that calls function to make sure no letters are allowed

On Validate:

docLevel.validateField(this.name

,docLevel.onlyNum,docLevel.onlyNumMsg);

 

Function is :

var

onlyNum = /^\d*$/;

var

onlyNumMsg = " Please enter only numeric values.";

function

validateField (getField, getRegex, getAlertMsg)

{

var

fieldFocus = xfa.resolveNode(getField);

var

regex = new RegExp();

regex.compile(getRegex);

var

runRegex = regex.test(fieldFocus.rawValue);

if

(fieldFocus.rawValue != null && runRegex == false)

{

app.alert(getAlertMsg

, 1, 0, "Numeric fields error");

fieldFocus.rawValue

= null;

xfa.host.setFocus(getField);

}

}

 

I have few questions:

1. why it does not allow me to enter e.g 23:22

2. why when I enter 2345 does not recognize it as a numeric value and give me an error message "

" Please enter only numeric values.";

What am I missing to be able to enter value like 1111 to have 11:11 or 23:11 correctly formated.Btw, how do I attache sample form I have to show the issue?

Avatar

Level 10

Hi,

you can have the validatation an easier way.

Just use time{KK:MM} for the validation pattern.

And, to allow the entry of values like 22:35 your change:Event script has to allow al length of 5 not 4.

Avatar

Former Community Member

Radzmar,

My time field allows input of even hours (i.e., 12:00) but does not accept 12:30 or 12:15. What am I missing? It works if I add format of time{KK:MM:SS}, but I don't want seconds.

Your suggestion (" Just use time{KK:MM} for the validation pattern.") to do validation through the validation pattern does not work for me, as it can be ignored by the end user and client is allowed to put anything. I have to make sure they enter it in format 12:30 or 23:15 and any other invalid input is cleared.

Thank you!