Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Javascript dropping trailing zero's on replace

Avatar

Level 2

Part of script I'm working on searches for a colon and removes it. It works great unless zero's are entered after the colon. I that case the zero's are dropped.

if

(TimeReceived.rawValue.search(":"))

val1

= TimeReceived.rawValue.replace(":", "");

else val1 = TimeReceived.rawValue;

For example:

15:15 = 1515

15:00 = 15

The missing zero's cause an issue later in the script. Is there a better way to do this or to add the zero's back in?

Thanks for any input on this issue.

1 Accepted Solution

Avatar

Correct answer by
Level 10

It's not the replace method dropping the trailing zeroes but the datetime field.

So you could change your line;

TimeReceived.rawValue.replace(":", "");

with

TimeReceived.rawValue.replace(

/^(\d\d)$/,"$100").replace(/:/,"")

Which will remove any colons or if there are only two digits add the trailing "00" back on.

Or you could use the formattedValue property which should be as displayed on the form

View solution in original post

14 Replies

Avatar

Former Community Member

What sort of a field are you using to hold the value - Text, Numeric, Date and Time?

Paul

Avatar

Level 2

Sorry, this is a Date/Time Field.

The user enters the time and it checks to see if it is in "working hours". It works unless the time entered ends in zero's because they get dropped then the : is removed. Ofcourse it also works if the time is entered with no :

I'm a noobie so I wouldn't be surprised if there is a better way to do this.

Avatar

Former Community Member

Just enter a display pattern of time{HH MM}. In th eObject palette/Value Tab click on Validation Pattern, then pick Display Pattern. This will give you a 24 hour time as well as all entries will have a minutes holder (even 00). The user can enter 15 or 1500 or 15:00 and it will always be displayed as 15 00. If you want a 12 hour clock use lower case hh. This way you do not need code.

Hope that helps

Paul

Avatar

Level 2

I am using the validation pattern you mentioned already, the issue is I need to check for "working hours" and it broke if the user entered a colon.

So I added a step to replace the colon, and that is where I run into the issue of the trailing zero's dropping off.

If the user types in 15:00 it fills the field fine but fails to check for "working hours". Here is the entire script, maybe it will make more sense:

if

(this.TimeReceived.isNull == true)

{

     xfa.host.messageBox("Please enter 'Time Received'");

     this.TimeReceived.rawValue

= null;

     xfa.host.setFocus("TimeReceived");

}

else if ((TimeofCall.Day.rawValue == 0) && (dayOfTheWeek.rawValue != "Fri"))

{

     if (this.TimeReceived.rawValue.search(":") > 0)

val1

= this.TimeReceived.rawValue.replace(":", "");

else

val1

= this.TimeReceived.rawValue;

     xfa.host.messageBox(val1);

     if (val1 < 600 || val1 > 1530)

{

     xfa.host.messageBox("If 'Time of Call' has Day selected, then the 'Time Received' must be within normal working hours:\n\nMonday Thursday:      06:00 to 15:30\nFriday: 06:00 to 14:30\n\nMake sure you are using military time, please re-enter.");

     this.TimeReceived.rawValue

= null;

     xfa.host.setFocus("TimeReceived");

}

}

else if ((TimeofCall.Day.rawValue == 0) && (dayOfTheWeek.rawValue == "Fri"))

{

     if (this.TimeReceived.rawValue.search(":") > 0)

     val1

= this.TimeReceived.rawValue.replace(":", "");

else

     val1

= this.TimeReceived.rawValue;

     if (val1 < 600 || val1 > 1430)

{

     xfa.host.messageBox("If 'Time of Call' has Day selected, then the 'Time Received' must be within normal working hours:\n\nMonday-Thursday: 06:00 to 15:30\nFriday: 06:00 to 14:30\n\nMake sure you are using military time, please re-enter.");

     this.TimeReceived.rawValue

= null;

xfa.host.setFocus("TimeReceived");

}

}

Avatar

Level 2

I wish I could download the attachment to type in these fileds and see how it actually works. It looks good but I'm not totally getting it.

My script was also checking day of the week (with your help) and I'm really close to it working perfectly except for the dropping zeros.

You can see in the screenshot attached that when I enter 12:00 the value is somehow changed to 12  (xfa.host.messageBox(val1);)

Is this a bug or is there anything I can do to get this working with my existing script?

I reeally appreciate your help!!!!

Avatar

Former Community Member

The queuing problem has been reported to Jive Software, again.

Please send a private message with your email address, or send an email to stwalker.adobe@gmail.com, I will forward the sample.

Avatar

Correct answer by
Level 10

It's not the replace method dropping the trailing zeroes but the datetime field.

So you could change your line;

TimeReceived.rawValue.replace(":", "");

with

TimeReceived.rawValue.replace(

/^(\d\d)$/,"$100").replace(/:/,"")

Which will remove any colons or if there are only two digits add the trailing "00" back on.

Or you could use the formattedValue property which should be as displayed on the form

Avatar

Level 2

Hi BR001,

Thanks for the suggestions. I tried changing the line you suggested and got the exact same result. It seems to add the zeros into the field and then strip them out again.

I also tried using formattedValue instead of rawValue and still the zeros are stripped away.

Any other ideas would really be appreciated.

- Kevin

Avatar

Former Community Member

I am confused ....I thought you wanted to enter a time and have it display as hh mm. If they enter it in hh:mm or hh format then using the pattern I described it will display as hh mm at all times (without any code). Am I missing something?

Paul

Avatar

Level 2

Hi pguerett,

It does display correctly, the problem is I'm am then checking "working hours" based on "day of the week". I need the code to produce an error and force them to re-enter if the time is not between 6:00 to 15:30 on Monday thru Thursday and 6:00 to 14:30 on Fridays.

If a colon is entered it fails at that step, so I'm trying to strip out the colon and at that point the trailing zeros are removed so 12:00 becomes 12

This only happens if it ends in zeros, so 12:15 becomes 1215 and works fine. I included the entire script in an earlier post, perhaps that will make more sense than my explanation. Sorry if this isn't clear.

Steve acknowledged this as a bug that was already reported so I'm trying to find a work around to get the same result.

- Kevin

Avatar

Former Community Member

Kevin,

Let me clarify. The queuing problem I referenced is related to attaching files to these email threads. It has nothing to do with your PDF form.

Steve

Avatar

Level 2

Steve,

Sorry, I completely misunderstood what you meant there. Doh!

Thanks for the clarification.

- Kevin

Avatar

Level 2

BR001,

You are correct, it works using formattedValue!

Not sure what I did wrong the first time I tried this, but I gave it another shot and it's working like so:

if

(TimeReceived.formattedValue.search(":") > 0)

val1

= TimeReceived.formattedValue.replace(":", "");

else

val1 = TimeReceived.formattedValue;

Thanks a million for the suggestion!

- Kevin