Copy link to clipboard
Copied
I have Adobe Acrobat 9 Standard. I created a fill-in form with a field called "State". It is a 2 character field allowing for the US State abbreviation. I would like a way make sure the user enters a valid US State abbreviation. Under Field PROPERTIES - FORMAT - CUSTOM KEYSTROKE SCRIPT, I already have this code to force Upper case letters. event.change = event.change.toUpperCase();
Here is a list of all of the US State abbreviations including the District of Columbia.
AL, AK, AZ, AR, CA, CO, CT, DE, DC, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WY
I am a total novice with this, so please also explain where I put the final script.
Thank you in advance for your help.
John.
Copy link to clipboard
Copied
You can use this as 'validate' script of same field:
var state = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"];
if(event.value){
var check = state.indexOf(event.value) > -1;
if(!check){
app.alert("Please enter correct US State abbreviation.");
event.rc = false;}}
Copy link to clipboard
Copied
Dear Nesa Nurani
Thank you soooo much for your help. With your suggestion, and a little modifying on my part, I came up with this, which I LOVE !!! Please try it out and feel free to share it with others.
I copied and pasted it into MS Word and added TAB marks to separate the columns. The TAB marks aren't showing in this copy, so I replaced all of the TAB marks with "@".
Thanks again, John.
var state = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"];
if(event.value){
var check = state.indexOf(event.value) > -1;
if(!check){
app.alert("Please enter correct US State abbreviation. \n \n
Alabama - AL@@Kentucky - KY@@North Dakota - ND\nAlaska - AK@@Louisiana - LA@@Ohio - OH\nArizona - AZ@@Maine - ME@@Oklahoma - OK\nArkansas - AR@@Maryland - MD@@Oregon - OR\nCalifornia - CA@@Massachusetts - MA@@Pennsylvania - PA\nColorado - CO@@Michigan - MI@@Rhode Island - RI\nConnecticut - CT@@Minnesota - MN@@South Carolina - SC\nDelaware - DE@@Mississippi - MS@@South Dakota - SD\nDistrict of Columbia - DC@Missouri - MO@@Tennessee - TN\nFlorida - FL@@Montana - MT@@Texas - TX\nGeorgia - GA@@Nebraska - NE@@Utah - UT\nHawaii - HI@@Nevada - NV@@Vermont - VT\nIdaho - ID@@@New Hampshire – NH@Virginia - VA\nIllinois - IL@@@New Jersey - NJ@@Washington - WA\nIndiana - IN@@New Mexico - NM@@West Virgini - WV\nIowa - IA@@@New York - NY@@Wisconsin - WI\nKansas - KS@@North Carolina - NC@@Wyoming - WY\n
");
event.rc = false;}}
Copy link to clipboard
Copied
Just a tip: This is harder than it at first appears. You need to allow a one character entry too, if you check when it is typed. Otherwise, nobody can enter a two character code, because they can't type the first character. This also means that later it may still be one character.
Copy link to clipboard
Copied
Hi Test Screen Name, Thank you for your response.
OK, Here is a unique list of all of the first letters in the abbreviations:
A, C, D, F, G, H, I, K, L, M, N, O, P, R, S, T, U, V, W,
And in case you need it,
Here is a unique list of all of the second letters in the abbreviations:
A, C, D, E, H, I, J, K, L, M, N, O, R, S, T, V, X, Y, Z,
But, it's the combination of BOTH letters for which I'd like it to check.
Basically, I'm trying to prevent someone from entering "ZZ", (for example) as a US State abbreviation.
Copy link to clipboard
Copied
You can use this as 'validate' script of same field:
var state = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"];
if(event.value){
var check = state.indexOf(event.value) > -1;
if(!check){
app.alert("Please enter correct US State abbreviation.");
event.rc = false;}}
Copy link to clipboard
Copied
Dear Nesa Nurani
Thank you soooo much for your help. With your suggestion, and a little modifying on my part, I came up with this, which I LOVE !!! Please try it out and feel free to share it with others.
I copied and pasted it into MS Word and added TAB marks to separate the columns. The TAB marks aren't showing in this copy, so I replaced all of the TAB marks with "@".
Thanks again, John.
var state = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"];
if(event.value){
var check = state.indexOf(event.value) > -1;
if(!check){
app.alert("Please enter correct US State abbreviation. \n \n
Alabama - AL@@Kentucky - KY@@North Dakota - ND\nAlaska - AK@@Louisiana - LA@@Ohio - OH\nArizona - AZ@@Maine - ME@@Oklahoma - OK\nArkansas - AR@@Maryland - MD@@Oregon - OR\nCalifornia - CA@@Massachusetts - MA@@Pennsylvania - PA\nColorado - CO@@Michigan - MI@@Rhode Island - RI\nConnecticut - CT@@Minnesota - MN@@South Carolina - SC\nDelaware - DE@@Mississippi - MS@@South Dakota - SD\nDistrict of Columbia - DC@Missouri - MO@@Tennessee - TN\nFlorida - FL@@Montana - MT@@Texas - TX\nGeorgia - GA@@Nebraska - NE@@Utah - UT\nHawaii - HI@@Nevada - NV@@Vermont - VT\nIdaho - ID@@@New Hampshire – NH@Virginia - VA\nIllinois - IL@@@New Jersey - NJ@@Washington - WA\nIndiana - IN@@New Mexico - NM@@West Virgini - WV\nIowa - IA@@@New York - NY@@Wisconsin - WI\nKansas - KS@@North Carolina - NC@@Wyoming - WY\n
");
event.rc = false;}}
Copy link to clipboard
Copied
Why not use a drop-down field with all the valid options, and save yourself (and the user) a lot of headaches?
Copy link to clipboard
Copied
Thank you for your suggestion. I did look into that option.
The reason I'm not using a drop-down field for the State is because there are no other drop-down fields within the entire 5 pages of forms, and I'd rather keep the consistency. If I were using drop-down fields in other places, then naturally, I would use one here also.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi,
Else, you can place this custom keystroke script which only allows to type the correct letters:
if (!event.willCommit) {
var aTester=event.value.split("");
aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
var testeChaine=aTester.join("");
var RegExState=/^((A(L|K|Z|R)?)|(C(A|O|T)?)|(D(E|C)?)|(FL?)|(GA?)|(HI?)|(I(D|L|N|A)?)|(K(S|Y)?)|(LA?)|(M(E|D|A|I|N|S|O|T)?)|(N(E|V|H|J|M|Y|C|D)?)|(O(H|K|R)?)|(PA?)|(RI?)|(S(C|D)?)|(T(N|X)?)|(UT?)|(V(T|A)?)|(W(A|V|I|Y)?))?$/;
event.rc=RegExState.test(testeChaine);
} else {
var RegExState=/^((A(L|K|Z|R))|(C(A|O|T))|(D(E|C))|(FL)|(GA)|(HI)|(I(D|L|N|A))|(K(S|Y))|(LA)|(M(E|D|A|I|N|S|O|T))|(N(E|V|H|J|M|Y|C|D))|(O(H|K|R))|(PA)|(RI)|(S(C|D))|(T(N|X))|(UT)|(V(T|A))|(W(A|V|I|Y)))?$/;
if (!(event.value=="" || RegExState.test(event.value))) {
event.rc=false;
app.alert("Please enter correct US State abbreviation. \n \nAlabama - AL\rKentucky - KY\rNorth Dakota - ND\nAlaska - AK\rLouisiana - LA\rOhio - OH\nArizona - AZ\rMaine - ME\rOklahoma - OK\nArkansas - AR\rMaryland - MD\rOregon - OR\nCalifornia - CA\rMassachusetts - MA\rPennsylvania - PA\nColorado - CO\rMichigan - MI\rRhode Island - RI\nConnecticut - CT\rMinnesota - MN\rSouth Carolina - SC\nDelaware - DE\rMississippi - MS\rSouth Dakota - SD\nDistrict of Columbia - DC Missouri - MO\rTennessee - TN\nFlorida - FL\rMontana - MT\rTexas - TX\nGeorgia - GA\rNebraska - NE\rUtah - UT\nHawaii - HI\rNevada - NV\rVermont - VT\nIdaho - ID\r New Hampshire – NH Virginia - VA\nIllinois - IL\r New Jersey - NJ\rWashington - WA\nIndiana - IN\rNew Mexico - NM\rWest Virgini - WV\nIowa - IA\r New York - NY\rWisconsin - WI\nKansas - KS\rNorth Carolina - NC\rWyoming - WY",3);
}
}
@+
Copy link to clipboard
Copied
Hi bebarth,
Thank you for your suggestion. My script works about the same way AND it shows the app.alert message when the user types in an invalid State abbreviation. I see the app.alert text in your script, but when I typed ZZ, it didn't bring up that alert.
Copy link to clipboard
Copied
Hi,
With my script you can't type ZZ because it only allows a correct combination of letters.
The alert message only appears if you try to leave the fields when you type only a single letter.
Try my example file to check...
You maybe place my script in a wrong place. It must be a custom keystroke script.
@+
Copy link to clipboard
Copied
@Nesa Nurani and @johnnyZ this is great and works well to accomplish what I was needing - I like the error message list with the correct ST options. I have a second entry of the same validation on the same page of a form (second address being entered and the script does not run the second time it is entered. is there something I have done wrong to have it reference the second instance. It was pasted separtely into each fields validation tab in properties?)
Copy link to clipboard
Copied
Share the script you are using or actual file.
Copy link to clipboard
Copied
Thanks @Nesa Nurani I used the script in the linked word doc above https://community.adobe.com/t5/acrobat-discussions/two-character-state-field-validation/td-p/1347353... from JohnyZ it worked great on first use in the doc, but for the second field it does nothing - not even an error notification. It acts as if it has already been implements in the field (from previous use). Not sure why. Maybe I need a different reference. Unfortunately the document is proprietary so I cannot share the actual doc, but it is the simple use of the script from JohnnyZ
Copy link to clipboard
Copied
Make sure the values are correct.
Copy link to clipboard
Copied
Thanks @Nesa Nurani I did have the values correct, but in my checking, I found a slight error in an extra period and that restricted the script. I guess I just needed to be told to review it again - again looking from a different angle. Thanks for being so responsive with the comments. All is working now as expected.
Copy link to clipboard
Copied
Hi TADvertising,
I'm happy to hear that you like my script. It actually is a conglomeration of a script I got on this forum and some changes that I made, like the app.alert message with the correct State abbreviations.