• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to make a simplified time entry (without hour and minute separator)

New Here ,
Jul 18, 2021 Jul 18, 2021

Copy link to clipboard

Copied

I want to enter the time in the time field in HMM format, but I want to display it in the same field in H:MM format.
Of course I want to be able to not enter H>23 and MM>59.
So 100 = 1:00 | 535 = 5:35 | 1000 = 10:00 | 2359 = 23:59 | 000 = 0:00 ... etc. and 060; 578; 2400; 3505... etc. = impossible.

TOPICS
How to , JavaScript , PDF forms

Views

456

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jul 19, 2021 Jul 19, 2021

Try this as "Validation" script of your field:

var e = event.value;
if(e.length == 3)event.value = e[0]+":"+e[1]+e[2];
else if(e.length == 4)event.value = e[0]+e[1]+":"+e[2]+e[3];
var y = event.value.split(":");
if(y[0] > 23 || y[1] > 59){
app.alert("Please enter correct time.",3);
event.value = "";}

Votes

Translate

Translate
Community Expert , Jul 19, 2021 Jul 19, 2021

Although I think most people are reluctant to use regular expressions, here is my suggestion:
In custom keystroke script:

if(!event.willCommit) {
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
var modeleRegEx=/^(([01]?[0-9]|2[0-3]){0,1}([0-5][0-9]?)?)?$/;
	event.rc=modeleRegEx.test(testeChaine);
} else {
	var modeleRegEx=/^([01]?[0-9]|2[0-3])[0-5][0-9]$/;
	event.rc=(event.value=="") || modeleRegEx.t
...

Votes

Translate

Translate
Community Expert ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Try this as "Validation" script of your field:

var e = event.value;
if(e.length == 3)event.value = e[0]+":"+e[1]+e[2];
else if(e.length == 4)event.value = e[0]+e[1]+":"+e[2]+e[3];
var y = event.value.split(":");
if(y[0] > 23 || y[1] > 59){
app.alert("Please enter correct time.",3);
event.value = "";}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Thanks a lot! It works perfectly!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Not quite... If you enter the ":" character it will duplicate it, and it will also accept non-numeric values (enter "zzzz", for example). It's missing a validation that the value entered is only digits.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Although I think most people are reluctant to use regular expressions, here is my suggestion:
In custom keystroke script:

if(!event.willCommit) {
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
var modeleRegEx=/^(([01]?[0-9]|2[0-3]){0,1}([0-5][0-9]?)?)?$/;
	event.rc=modeleRegEx.test(testeChaine);
} else {
	var modeleRegEx=/^([01]?[0-9]|2[0-3])[0-5][0-9]$/;
	event.rc=(event.value=="") || modeleRegEx.test(event.value);
	if (!event.rc) app.alert("The correct syntax of the input time must be HMM.",3);
}

and in custom format script:

var theValue=event.target.valueAsString;
event.value=theValue.substring(0, theValue.length-2)+":"+theValue.substring(theValue.length-2);

@+

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

LATEST

It also works very well, although writing is more complicated. Thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines