These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Adding validation to...

Anpumes
Registered: Feb 3 2011
Posts: 7
Answered

I want to start off by thanking all those who have helped me directly or indirectly. I'm really new to JavaScript for Acrobat, and was not very familiar with Coding before starting this project.
 
Now down to what I'm trying to solve.
 
I have a Text Field that is driven by a ComboBox (Thanks to Thom, I got this working perfectly), and it also has Custom Format Script, Custom Keystroke Script and Custom Calculation Script running on it to provide the functionality that I'm after. This is working exactly as intended until I try to validate what the user is allowed to input, I've tried a variety of ways, and can't seem to get this to work on my own, so I am humbly seeking assistance again.
 
Here is what I have starting with the Keystroke Script:
 

if(!event.willCommit)				//calling individual keystrokes and selections
	{
	event.target.bDetect=true;
	}
else if(event.value=="")
	{
	event.target.bDetect=false;
	}

  
Then my calculation script:
var Class = this.getField("cmbClass_1").value;			//setting variable then calling function
&nbsp;<br />
event.rc = !event.target.bDetect;
	{
	event.value = Value(Class);
	}

  
And finally the script I would like to run, which will validate it as a number and that it fits specific criteria. I can't figure out how to integrate this to make it work properly. I can get it to work, but it'll either prompt me when I select from the ComboBox which drives it or it prompts before the focus is on the Text Field to be validated. I'm sure it probably just a simple solution, and I'm over thinking it at this point, so a fresh set of eyes on this would be wonderful.
 
event.rc = !/[^-0-9]+/.test(event.value);
if (event.value != 6 && event.value != 8 && event.value != 10 && event.value != 12)
	{
	app.alert ("Invalid value: Value must be 6, 8, 10, or 12");
	event.rc = false;
	}

 
=============================================================================================================
=============================================================================================================
New code:
function Sum(nValues)								//sum the number of values that are numbers excluding any blanks or nulls
&nbsp;<br />
{
var nSum = 0									//variable for the sum
&nbsp;<br />
for (i = 0; i < nValues.length; i++)						//looping through fields
	{
	if ((nValues[i] != "") & (nValues[i] != " ") & (!isNaN(nValues[i])))	//testing for blanks, nulls and Not a Numbers
	console.println ("For i = " + i + " -- nValues = " + nValues[i])
		{
		nSum += Number (nValues[i])					//sums numbers
		if ((nSum == 0) & (nValues[0] == ""))				//testing for zero and null first value
			{
			nSum = ""						//sums equal zero, setting to null
			}
		}
	}
return nSum									//returning the nSum value
}

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
One of the problems you are seeing is the order of event execution. The validation script on the text field is being called when the text field is set from the combobox. It must be setup to either allow values set by the combo or be blocked from running when this value is set. For example, if the readonly property is set early on the text field when driven from the combobox, then this property could be used as a blocking variable for the validation.

However, you are trying to manipulate a complex series of events between two fields. There are two things you need to do:
1. Get a handle on the order of even execution. Place console.println statments in all conditions on all events so you can see what's going on.

2. Simplify the event interaction. If the system is driven entirely from the combobox, then don't use the calculate or keystroke events on the text field.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Anpumes
Registered: Feb 3 2011
Posts: 7
Thank you Thom,

I've been using the "console.println" statements to trace the order in which various events execute. I have learned the "Format: Custom Keystroke script" executes before anything else does, when the field that executes it is manipulated. This is preempting any event values that might be calculated from other sources. I have figured out how to override user input data when a value is selected from the combobox. This had made some things work, but it is also bogging down the document as it is forcing multiple processing of the values associated with the override values.

As best as I can tell there is no way around this, outside of getting rid of the Keystroke all together. This then makes it nearly impossible for the readonly property to be set based on the selection from the combobox driving the field.

It seems that I have a decision to make regarding this form. Keep the Keystroke script which sets the readonly propery, or remove it and simply use the calculate fields with no readonly property.


I did come across one really bizarre situation (? bug ?) related to the "console.println" statement. Maybe someone can either explain to me what is going on or, find a user error that I am not seeing, and the JavaScript Debugger Console is not catching either. With the following code:

function Sum(nValues) //sum the number of values that are numbers excluding any blanks or nulls {var nSum = 0 //variable for the sum for (i = 0; i < nValues.length; i++) //looping through fields{if ((nValues[i] != "") & (nValues[i] != " ") & (!isNaN(nValues[i]))) //testing for blanks, nulls and Not a Numbersconsole.println ("For i = " + i + " -- nValues = " + nValues[i]){nSum += Number (nValues[i]) //sums numbersif ((nSum == 0) & (nValues[0] == "")) //testing for zero and null first value{nSum = "" //sums equal zero, setting to null}}}return nSum //returning the nSum value}
If I remove the following line or put "//" in front of it:

console.println ("For i = " + i + " -- nValues = " + nValues[i])
then my whole statement above stops returning the null value and will only return the number 0.

After much trying, i can't get the code to display properly. What a pain! Thanks for any help.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I can't see the code properly, it's all in one line. But it appears that the code blocks in your "Sum" function are not correctly bracketed.

It also seems that you are over complicating the control of the text field. Events are spawning new events. I do not believe that any of this code is needed on text field.




Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Anpumes
Registered: Feb 3 2011
Posts: 7
I don't know why it isn't showing the code laid out in the format that I posted it, but I went back and looked at my code again. All of my "{}" and "()" are in opening and closing pairs. I can only imagine it is user error, but I can't seem to locate it. I guess it isn't really a big deal for this "console.println" to be in the code, I just don't understand why it changes my output when I remove it.

EDIT: I added the code that is giving me problems at the end of my original post, for some reason that is the only way it shows up properly, otherwise it just runs out in a long string.

thomp wrote:
It also seems that you are over complicating the control of the text field. Events are spawning new events. I do not believe that any of this code is needed on text field.
So what your saying to have no code at all on the text field, have it fully driven by the combobox?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Anpumes wrote:
So what your saying to have no code at all on the text field, have it fully driven by the combobox?
Yes.

I haven't analyzed your form and the intended functionality. But if at all possible, it is the best design practice to drive all actions from a single stable source. Dealing with multiple interacting asynchronous events can cause terrible problems. You have to be very careful and have a good handle on the system before getting this type of thing to work well. Simplify whenever possible. And try not to work with actions that create feedback.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Anpumes
Registered: Feb 3 2011
Posts: 7
Thank you Thom, and others. You've all been a great help either directly or indirectly.