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

Two character State field validation

Community Beginner ,
Jan 08, 2023 Jan 08, 2023

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.

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF forms

Views

438

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 , Jan 08, 2023 Jan 08, 2023

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
...

Votes

Translate

Translate
Community Beginner , Jan 08, 2023 Jan 08, 2023

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", "I

...

Votes

Translate

Translate
LEGEND ,
Jan 08, 2023 Jan 08, 2023

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. 

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 Beginner ,
Jan 08, 2023 Jan 08, 2023

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.

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 ,
Jan 08, 2023 Jan 08, 2023

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;}}

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 Beginner ,
Jan 08, 2023 Jan 08, 2023

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;}}

States.jpg

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 ,
Jan 08, 2023 Jan 08, 2023

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?

 

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 Beginner ,
Jan 08, 2023 Jan 08, 2023

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.

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 Beginner ,
Jan 08, 2023 Jan 08, 2023

Copy link to clipboard

Copied

Here is the MS Word document with the TAB marks.

Simply copy and paste this into the Custom Validation Script.

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 ,
Jan 08, 2023 Jan 08, 2023

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);
	}
}

@+

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 Beginner ,
Jan 08, 2023 Jan 08, 2023

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.

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 ,
Jan 08, 2023 Jan 08, 2023

Copy link to clipboard

Copied

LATEST

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.

Capture d’écran 2023-01-09 à 08.17.35.png

@+

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