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

Split Field Name based on Regex Query

Engaged ,
Jun 10, 2021 Jun 10, 2021

Is it possible to split a field name that was declared using a regex query? I'm trying to find the field name then split the field name to extract the number to then use that to hide/show form elements.

 

The debugger gets held up on card.split and tells me it's not a function. I tried several things to no avail.

 

//set all module selection test regex
	for (var i = 0; i < this.numFields; i++) {
		var fname = this.getNthFieldName(i);
		var f = this.getField(fname);
		var mods = /modules-\den/;
		if (f.name.match(mods)) {
			f.setAction("MouseUp",
				'for (var i = 0; i < this.numFields; i++) {\n' +
					'var fname = this.getNthFieldName(i);\n' +
					'var f = this.getField(fname);\n' +
					'var mNum = event.targetName;\n' +
					'var card = /card\\d/;\n' +
					'var cardT = /card-\\d/;\n' +
					'var reset = /reset\\d/;\n' +
					'var gfx = /enclosure\\dm/;\n' +
					'var ary8 = "12345678";\n' +
					'var mSplit = mNum.split("-");\n' +
					'var mName0 = mSplit[0];\n' +
					'var mName1 = mSplit[1];\n' +
					'var sMod = mName1.split("e");\n' +
					'var modName = sMod[0];\n' +
					'var cSplit = card.split("d");\n' +
					'var cName0 = cSplit[0];\n' +
					'var cName1 = cSplit[1];\n' +
					'var cTSplit = cardT.split("-");\n' +
					'var cTName0 = cTSplit[0];\n' +
					'var cTName1 = cTSplit[1];\n' +
					'var rSplit = reset.split("t");\n' +
					'var rName0 = rSplit[0];\n' +
					'var rName1 = rSplit[1];\n' +
					'var gSplit = gfx.split("e");\n' +
					'var gName0 = gSplit[0];\n' +
					'var gName1 = gSplit[1];\n' +
					'var gnSplit = gName1.split("m");\n' +
					'var gName = gnSplit[0];\n' +
					'if(modName.value == "2") {\n' +
						'sReset.display = display.visible;\n' +
						'if(f.name.match(card) && cName1.value == ary8[0,1] && rname1.value == ary8[0,1]) {\n' +
							'card.display = display.visible;\n' +
							'cardT.display = display.visible;\n' +
							'reset.display = display.visible;\n' +
						'}\n' +
						'else if(f.name.match(card) && cName1.value == ary8[2-7] && rname1.value == ary8[2-7]) {\n' +
							'card.display = display.hidden;\n' +
							'cardT.display = display.hidden;\n' +
							'reset.display = display.hidden;\n' +
						'}\n' +
						'if(f.name.match(gfx) && gName.value == ary8[0]) {\n' +
							'gfx.display = display.visible;\n' +
						'}\n' +
						'else if(f.name.match(gfx) && gName.value == ary8[1-3]) {\n' +
							'gfx.display = display.hidden;\n' +
						'}\n' +
					'}\n' +
					'else if(modName.value == "4") {\n' +
						'sReset.display = display.visible;\n' +
						'if(f.name.match(card) && cName1.value == ary8[0-3] && rname1.value == ary8[0-3]) {\n' +
							'card.display = display.visible;\n' +
							'cardT.display = display.visible;\n' +
							'reset.display = display.visible;\n' +
						'}\n' +
						'else if(f.name.match(card) && cName1.value == ary8[4-7] && rname1.value == ary8[4-7]) {\n' +
							'card.display = display.hidden;\n' +
							'cardT.display = display.hidden;\n' +
							'reset.display = display.hidden;\n' +
						'}\n' +
						'if(f.name.match(gfx) && gName.value == ary8[1]) {\n' +
							'gfx.display = display.visible;\n' +
						'}\n' +
						'else if(f.name.match(gfx) && gName.value == ary8[0,2,3]) {\n' +
							'gfx.display = display.hidden;\n' +
						'}\n' +
					'}\n' +
					'else if(modName.value == "6") {\n' +
						'sReset.display = display.visible;\n' +
						'if(f.name.match(card) && cName1.value == ary8[0-5] && rname1.value == ary8[0-5]) {\n' +
							'card.display = display.visible;\n' +
							'cardT.display = display.visible;\n' +
							'reset.display = display.visible;\n' +
						'}\n' +
						'else if(f.name.match(card) && cName1.value == ary8[6,7] && rname1.value == ary8[6,7]) {\n' +
							'card.display = display.hidden;\n' +
							'cardT.display = display.hidden;\n' +
							'reset.display = display.hidden;\n' +
						'}\n' +
						'if(f.name.match(gfx) && gName.value == ary8[2]) {\n' +
							'gfx.display = display.visible;\n' +
						'}\n' +
						'else if(f.name.match(gfx) && gName.value == ary8[0,1,3]) {\n' +
							'gfx.display = display.hidden;\n' +
						'}\n' +
					'}\n' +
					'else if(modName.value == "8") {\n' +
						'sReset.display = display.visible;\n' +
						'if(f.name.match(card) && cName1.value == ary8[0-7] && rname1.value == ary8[0-7]) {\n' +
							'card.display = display.visible;\n' +
							'cardT.display = display.visible;\n' +
							'reset.display = display.visible;\n' +
						'}\n' +
						'if(f.name.match(gfx) && gName.value == ary8[3]) {\n' +
							'gfx.display = display.visible;\n' +
						'}\n' +
						'else if(f.name.match(gfx) && gName.value == ary8[0-2]) {\n' +
							'gfx.display = display.hidden;\n' +
						'}\n' +
					'}\n' +
				'}'
			);
		}
	}

 

TOPICS
Create PDFs , JavaScript , PDF forms
1.3K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Jun 10, 2021 Jun 10, 2021

Your script is not readable.

View solution in original post

Translate
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 ,
Jun 10, 2021 Jun 10, 2021

May be better when you use a function at document level. And use the function at the button.

In a function the script is better readable and it is easier to debug the script.

Translate
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
Engaged ,
Jun 10, 2021 Jun 10, 2021

I'll give it a try!

Translate
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
Engaged ,
Jun 10, 2021 Jun 10, 2021

I tried it but no luck.

Prior to posting this, I felt like maybe I wasn't actually putting anything in the script to actually declare the fields other than defining the variables and queries, so I tried to add:

if(f.name.match(card) [ | | all other regex definitions that have split] ) { all split variables.

I was thinking maybe if the script was actually working to find the field names then adding the variables that split the field name when called it might work. It didn't and produced the same error, so it was removed.

My other thought is if I could get the regex query field names as a string, maybe it would then want to split the field name.

 

	this.addScript("modSel",
		'function modSelect() {\n' +
		'for (var i = 0; i < this.numFields; i++) {\n' +
			'var fname = this.getNthFieldName(i);\n' +
			'var f = this.getField(fname);\n' +
			'var mNum = event.targetName;\n' +
			'var card = /card\\d/;\n' +
			'var cardT = /card-\\d/;\n' +
			'var reset = /reset\\d/;\n' +
			'var gfx = /enclosure\\dm/;\n' +
			'var ary8 = "12345678";\n' +
			'var mSplit = mNum.split("-");\n' +
			'var mName0 = mSplit[0];\n' +
			'var mName1 = mSplit[1];\n' +
			'var sMod = mName1.split("e");\n' +
			'var modName = sMod[0];\n' +
			'var cSplit = card.split("d");\n' +
			'var cName0 = cSplit[0];\n' +
			'var cName1 = cSplit[1];\n' +
			'var cTSplit = cardT.split("-");\n' +
			'var cTName0 = cTSplit[0];\n' +
			'var cTName1 = cTSplit[1];\n' +
			'var rSplit = reset.split("t");\n' +
			'var rName0 = rSplit[0];\n' +
			'var rName1 = rSplit[1];\n' +
			'var gSplit = gfx.split("e");\n' +
			'var gName0 = gSplit[0];\n' +
			'var gName1 = gSplit[1];\n' +
			'var gnSplit = gName1.split("m");\n' +
			'var gName = gnSplit[0];\n' +
			'if(modName.value == "2") {\n' +
				'sReset.display = display.visible;\n' +
				'if(f.name.match(card) && cName1.value == ary8[0,1] && rname1.value == ary8[0,1]) {\n' +
					'card.display = display.visible;\n' +
					'cardT.display = display.visible;\n' +
					'reset.display = display.visible;\n' +
				'}\n' +
				'else if(f.name.match(card) && cName1.value == ary8[2-7] && rname1.value == ary8[2-7]) {\n' +
					'card.display = display.hidden;\n' +
					'cardT.display = display.hidden;\n' +
					'reset.display = display.hidden;\n' +
				'}\n' +
				'if(f.name.match(gfx) && gName.value == ary8[0]) {\n' +
					'gfx.display = display.visible;\n' +
				'}\n' +
				'else if(f.name.match(gfx) && gName.value == ary8[1-3]) {\n' +
					'gfx.display = display.hidden;\n' +
				'}\n' +
			'}\n' +
			'else if(modName.value == "4") {\n' +
				'sReset.display = display.visible;\n' +
				'if(f.name.match(card) && cName1.value == ary8[0-3] && rname1.value == ary8[0-3]) {\n' +
					'card.display = display.visible;\n' +
					'cardT.display = display.visible;\n' +
					'reset.display = display.visible;\n' +
				'}\n' +
				'else if(f.name.match(card) && cName1.value == ary8[4-7] && rname1.value == ary8[4-7]) {\n' +
					'card.display = display.hidden;\n' +
					'cardT.display = display.hidden;\n' +
					'reset.display = display.hidden;\n' +
				'}\n' +
				'if(f.name.match(gfx) && gName.value == ary8[1]) {\n' +
					'gfx.display = display.visible;\n' +
				'}\n' +
				'else if(f.name.match(gfx) && gName.value == ary8[0,2,3]) {\n' +
					'gfx.display = display.hidden;\n' +
				'}\n' +
			'}\n' +
			'else if(modName.value == "6") {\n' +
				'sReset.display = display.visible;\n' +
				'if(f.name.match(card) && cName1.value == ary8[0-5] && rname1.value == ary8[0-5]) {\n' +
					'card.display = display.visible;\n' +
					'cardT.display = display.visible;\n' +
					'reset.display = display.visible;\n' +
				'}\n' +
				'else if(f.name.match(card) && cName1.value == ary8[6,7] && rname1.value == ary8[6,7]) {\n' +
					'card.display = display.hidden;\n' +
					'cardT.display = display.hidden;\n' +
					'reset.display = display.hidden;\n' +
				'}\n' +
				'if(f.name.match(gfx) && gName.value == ary8[2]) {\n' +
					'gfx.display = display.visible;\n' +
				'}\n' +
				'else if(f.name.match(gfx) && gName.value == ary8[0,1,3]) {\n' +
					'gfx.display = display.hidden;\n' +
				'}\n' +
			'}\n' +
			'else if(modName.value == "8") {\n' +
				'sReset.display = display.visible;\n' +
				'if(f.name.match(card) && cName1.value == ary8[0-7] && rname1.value == ary8[0-7]) {\n' +
					'card.display = display.visible;\n' +
					'cardT.display = display.visible;\n' +
					'reset.display = display.visible;\n' +
				'}\n' +
				'if(f.name.match(gfx) && gName.value == ary8[3]) {\n' +
					'gfx.display = display.visible;\n' +
				'}\n' +
				'else if(f.name.match(gfx) && gName.value == ary8[0-2]) {\n' +
					'gfx.display = display.hidden;\n' +
				'}\n' +
			'}\n' +
		'}\n' +
		'}'
	)

	for (var i = 0; i < this.numFields; i++) {
		var fname = this.getNthFieldName(i);
		var f = this.getField(fname);
		var mods = /modules-\den/;
		if (f.name.match(mods)) {
			f.setAction("MouseUp", "modSelect();");
		}
	}

 

Translate
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 ,
Jun 10, 2021 Jun 10, 2021

Your script is not readable.

Translate
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
Engaged ,
Jun 16, 2021 Jun 16, 2021
LATEST

I wound up changing field names and going an entirely different route and abandoning the split idea when I could just group them based on like field numbers I added to the end of the field name.

Translate
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 ,
Jun 10, 2021 Jun 10, 2021

This is not valid JS notation for a string:

ary8[0,1]

Nor is this (for anything):

ary8[2-7]

The former will only return the first letter in the string. If you want to return a sub-string then use the substring method, like this:

ary8.substring(0,2);

This will return the first two characters of the string.

Translate
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