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.
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 = "";}
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
...
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 = "";}
Copy link to clipboard
Copied
Thanks a lot! It works perfectly!
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.
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);
@+
Copy link to clipboard
Copied
It also works very well, although writing is more complicated. Thank you very much!