Skip to main content
Participant
November 9, 2022
Answered

How to use keystroke to mimic an arbitrary mask? Trying to recreate 999-9999-9999[^a-zA-Z]{0,1}

  • November 9, 2022
  • 2 replies
  • 824 views

I'm trying to create a field that allows you to enter numbers without dashes but displays and enters like an arbitrary mask with the following format 999-9999-9999[^a-zA-Z]{0,1}, where the last character is not required but only allows for one(1) character if entered.  

I have tried a mask but the last character is alluding me.  I have tried 999-9999-9999X which works but requires me to enter something for that last character, if only a space.  I am not very familiar with keystroke scripting and think this might be the answer, possibly with a validation script too.  I could use some direction. 

Thanks

This topic has been closed for replies.
Correct answer bebarth

Hi,

To complete the Thom's format script, you can write:

if (/^\d{11}$/.test(event.value)) event.value=util.printx("999-9999-9999", event.value);
else if (/^\d{11}[a-zA-Z]$/.test(event.value)) event.value=util.printx("999-9999-9999X", event.value);
else event.value="";

But this only check and format the text at the end of the typing.
So, you can write a custom keystroke script which will check the right format while typing:

if (!event.willCommit) {
	if (/^\d{2}$/.test(event.value) && event.selStart==2) event.change+="-";
	else if (/^\d{3}-\d{3}$/.test(event.value) && event.selStart==7) event.change+="-";
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
	event.rc=/^(\d{0,3}(-\d{0,4}(-\d{0,4}[a-zA-Z]?)?)?)?$/.test(testeChaine);
} else {
	event.rc=event.value=="" || /^\d{3}-\d{4}-\d{4}[a-zA-Z]?$/.test(event.value);
}

This script will automatically place the hyphens without type them.

@+

2 replies

Participant
December 9, 2022

Thank you works great.  Anywhere I can read up more on creating these types of scripting? 

 

bebarth
Community Expert
Community Expert
December 9, 2022

I should write a sheet about that soon... I had several request!

I will let you know.

@+

Participant
November 9, 2022

Example.  I want to be able to type 11122223333 or 11122223333j or 11122223333H and it will display it as 111-2222-3333, 111-2222-3333j, or 111-2222-3333H.  However if I try to type more characters it'll not error, just does not allow it.  

Thom Parker
Community Expert
Community Expert
November 10, 2022

A simple way to do this is to use a custom format script. Formatting is applied after the field data is committed, so the dashes are not applied while the user is entering the data, but after they hit return. And the actual entered data is not modified. Formatting only affects how the data is displayed. 

event.value = util.printx("999-9999-9999", event.value);

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often