Skip to main content
Inspiring
November 29, 2022
Answered

Simplified DTG Validation Script

  • November 29, 2022
  • 2 replies
  • 21565 views

Aaaannndd... I'm back, yet again.  I've been at this months and still feel no better or more intelligent regarding scripting in Acrobat.  At this rate it will be decades before I'm even somewhat proficient -- or possibly longer. 

 

I'm still attempting to write a validation script for just a Military Time field that will error out with any incorrect input. 

This is what I've worked out thus far and now trying to figure out how to have the field reject any input that doesn't follow these guidelines -- to include anyone attempting to use a colon seperator or numbers outside a 23 hour 59 minute clock:

$('#startTimeInput').jqxDateTimeInput({
formatString : "HHMM" + timeCodeLetter .toUpperCase()
});

 

As my posts should make it clear, I have absolutely no idea what I'm doing.  When I get something to work without the expertise here, in the Navy we call this "FM" or "f*ckin' magic". while estatic, it certainly doesn't feel like I've learned anything.  Thankfully, there's no FM involved with the SME's here.  My thanks in advance for any and all assistance.

 

I've been working virtually the same script for a full DTG and plan on applying everything learned in this thread to the following script as well:

 

$('#startTimeInput').jqxDateTimeInput({
formatString : "ddHHMM" + timeCodeLetter + "mmm yyyy"  .toUpperCase()
});

FYSA:  For anyone stumbling upon this, as I've seen it discussed in many different threads, there is no 2400 or 0000 used in Military Time -- it's always a "lost" minute where all official log entries are either 2359 or 0001.

This topic has been closed for replies.
Correct answer bebarth

...and as required, here it is for only the time:

// 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 RegExTime=/^((((0|1)\d?)|([2][0-3]?))([0-5]\d?)?)?$/;
	event.rc=RegExTime.test(testeChaine) && testeChaine!="24" && Number(testeChaine.substr(2,1))<=5 && testeChaine!="0000";
} else {
	var RegExTime=/^(((0|1)\d)|([2][0-3]))([0-5]\d)$/;
	event.rc=event.value=="" || (RegExTime.test(event.value) && event.value!="0000");
}

// Validate script
event.rc=event.value=="" || (/^((((0|1)\d)|([2][0-3]))([0-5]\d))?$/.test(event.value) && event.value!="0000");

@+

 

2 replies

bebarth
Braniac
December 9, 2022

Hi,

You reported a malfunction for my script:

quote
.. any time the DTG (full format ddHHMM(z) mmm yyyy) has a day (dd) entry of "10", "20", or "30", the script will not allow a time entry to exceed two (2) zeroes. So only the HH can contain 00 and then it will not allow a third zero as would be required within the first nine (9) minutes after midnight to 0009 (technically 0001 through 0009 as we've previously discussed there is no 0000 or 2400 used).

And effectively, it's a case I didn't think about!

Here is a new script which will solve this mistake:

if (!event.willCommit) {
	event.change=event.change.toUpperCase();
	if ((event.value.length==6 && event.selStart==6) || (event.selStart==6 && event.selEnd>7)) event.change+=" ";
	else if ((event.value.length==10 && event.selStart==10) || (event.selStart==10 && event.selEnd>11)) event.change+=" 20";
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
	var RegExFullMiltaryDTG=/^(((0[1-9]?)|((1|2)\d?)|(3[01]?))((((0|1)\d?)|(2[0-3]?))([0-5]\d?)?([A-Z]([ ]((((J(AN?)?)|(F(EB?)?)|(M(AR?)?)|(A(PR?)?)|(M(AY?)?)|(J(UN?)?)|(J(UL?)?)|(A(UG?)?)|(S(EP?)?)|(O(CT?)?)|(N(OV?)?)|(D(EC?)?)))([ ](2(0\d{0,2})?)?)?)?)?)?)?)?$/;
	var theFullTest=RegExFullMiltaryDTG.test(testeChaine) && testeChaine.indexOf("00")!=0 && Number(testeChaine.substr(0,2))<32 && testeChaine.indexOf("24")!=2 && (testeChaine.indexOf("0000")==-1 || (testeChaine.indexOf("0000")==1 && testeChaine.indexOf("00000")==-1)) && !isNaN(Number(testeChaine.substr(0,6)));
	var dayOK=1;
	if (testeChaine.length>11) dayChecking(testeChaine);
	event.rc=theFullTest && dayOK;
} else {
	var RegExFullMiltaryDTG=/^(((0[1-9])|((1|2)\d)|(3[01]))(((0|1)\d)|(2[0-3]))([0-5]\d)[A-Z][ ]((JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OCT)|(NOV)|(DEC))( 20)\d{2})?$/;
	var dayOK=1;
	if (RegExFullMiltaryDTG.test(event.value)) dayChecking(event.value);
	event.rc=dayOK;
}

You should be able to write that case correctly now.

Don't hesitate to tell me if you catch an other malfunction.

@+

bebarth
bebarthCorrect answer
Braniac
December 12, 2022

...and as required, here it is for only the time:

// 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 RegExTime=/^((((0|1)\d?)|([2][0-3]?))([0-5]\d?)?)?$/;
	event.rc=RegExTime.test(testeChaine) && testeChaine!="24" && Number(testeChaine.substr(2,1))<=5 && testeChaine!="0000";
} else {
	var RegExTime=/^(((0|1)\d)|([2][0-3]))([0-5]\d)$/;
	event.rc=event.value=="" || (RegExTime.test(event.value) && event.value!="0000");
}

// Validate script
event.rc=event.value=="" || (/^((((0|1)\d)|([2][0-3]))([0-5]\d))?$/.test(event.value) && event.value!="0000");

@+

 

BarlaeDC
Participating Frequently
November 29, 2022

Hi,

 

I am not sure if this will help or not, but if I was looking to limit a field to a specific range of numbers, I would use a RegEx to achieve this, placing this in a custom script on the validation would mean that a user could not enter any value that does not pass the validation check.

 

The code I would use is

// I am not a regex expert and I am sure someone could simplify this down, but this is the basic of it
// ([0]{3}[1-9]) - makes sure 0000 is not allowed 
// ([0][12][0-5][0-9]) - if it does start with a 0 then it can only be a 1 or a 2 after it, followed by 0-5 and then by 0-9
//([1][0-9][0-5][0-9]) - if it starts with a 1, then it can be followed by 0-9, then 0-5, then 0-9 again.
// ([2][0-3][0-5][0-9]) - if it starts with a 2, it can only be followed by a 0,1,2,3 and then 0-5 and 0-9
var myRegExp = new RegExp("([0]{3}[1-9])|([0][12][0-5][0-9])|([1][0-9][0-5][0-9])|([2][0-3][0-5][0-9])");
event.rc = myRegExp.test ( event.value);

 I hope this helps

Inspiring
November 29, 2022

THANK YOU!!  YOU ARE A ROCKSTAR! 🙂  It's working quite well and I will now attempt to use it as a baseline for adding the full DTG (ddHHMM(z) mmm yyyy).  I truly appreciate the assist.

bebarth
Braniac
November 29, 2022

Hi,

You can write a validation script:

event.rc=event.value=="" || (/^((((0|1)\d)|([2][0-3]))([0-5]\d))?$/.test(event.value) && event.value!="0000");

or a custom keystroke script to manage the typing:

if (!event.willCommit) {
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
	var RegExTime=/^((((0|1)\d?)|([2][0-3]?))([0-5]\d?)?)?$/;
	event.rc=RegExTime.test(testeChaine) && testeChaine!="24" && testeChaine!="0000";
} else {
	var RegExTime=/^((((0|1)\d)|([2][0-3]))([0-5]\d))?$/;
	event.rc=event.value=="" || (RegExTime.test(event.value) && event.value!="0000");
}

...but for my information, what is the time indicated for midnight?

@+