Copy link to clipboard
Copied
I've found a few conversations on validating but can't find any written resolutions other than "oh hey, I fixed it".
The code I use to lock the doc fields is:
var r = app.alert("Are you ready to lock this document?",2,2);
if (r == 4) { // 4 is Yes, 3 is No
for (var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
this.getField(fname).readonly = true; // makes all fields readonly
}
}
A script I found to check validation looks like this:
// These are the required fields on the form.
// Populate array with their names.
var RequiredFields = new Array(3);
RequiredFields[0] = "Captain";
RequiredFields[1] = "Equipment";
RequiredFields[2] = "Date";
// These are the alert messages shown when a required field is empty.
// Populate array with messages.
// Make sure there's one message for each required field.
var alertMsg = new Array(3);
alertMsg[0] = "Please enter the name of the Captain.";
alertMsg[1] = "Please enter the Equipment ID.";
alertMsg[2] = "Please enter the Date.";
var bSuccess=True
var emptyTest = /^\s*$/;
var fieldCount = RequiredFields.length
var fld = 0;
for (var i=0; i < fieldCount; i++)
{
fld = this.getField(RequiredFields);
if( emptyTest.test(fld.value) ) // if required field is empty
{
bSuccess=false;
app.alert(alertMsg);
fld.setFocus();
break;
}
if(bSuccess)
this.mailDoc(true);
}
Any ideas how I can combine them so that it validates the 3 fields and if good asks if you want to lock it?
Copy link to clipboard
Copied
I was able to get a makeshift version of it to work but it looks and acts clunky:
//Validation code
var fld1 = this.getField('Captain');
var fld2 = this.getField('Date');
var fld3 = this.getField('Equipment');
{
if (fld1.value == "")
{
app.alert("Please enter the name of the Captain.");
fld1.setFocus();
}
if (fld2.value == "")
{
app.alert("Please enter the Date. mm/dd/yy");
fld2.setFocus();
}
if (fld3.value == "")
{
app.alert("Please enter the Equipment ID.");
fld3.setFocus();
}
if (fld1.value != "" && fld2.value != "" && fld3.value != "")
// Code that completes submission
{
var r = app.alert("Are you ready to lock this document?",2,2);
if (r == 4) { // 4 is a Yes answer
for (var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
this.getField(fname).readonly = true; // makes all fields readonly
}
app.execMenuItem("SaveAs");
}
if (r == 3) { // 3 is a No answer
app.execMenuItem("SaveAs");
}
}
}
Copy link to clipboard
Copied
After blowing apart the original for loop, I was able to get it to work like I wanted but the script still looks ugly, oh well, it works exactly how I wanted it to...
Final script:
//Validation code
var fld1 = this.getField('Captain');
var fld2 = this.getField('Date');
var fld3 = this.getField('Equipment');
{
if (fld1.value == "")
{
app.alert("Please enter the name of the Captain.");
fld1.setFocus();
}
else {
if (fld2.value == "")
{
app.alert("Please enter the Date. mm/dd/yy");
fld2.setFocus();
}
else {
if (fld3.value == "")
{
app.alert("Please enter the Equipment ID.");
fld3.setFocus();
}
else {
if (fld1.value != "" && fld2.value != "" && fld3.value != "")
// Code that completes submission
{
var r = app.alert("Are you ready to lock this document?",2,2);
if (r == 4) { // 4 is a Yes answer
for (var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
this.getField(fname).readonly = true; // makes all fields readonly
}
app.execMenuItem("SaveAs");
}
if (r == 3) { // 3 is a No answer
app.execMenuItem("SaveAs");
}
}
}
}
}
}
Copy link to clipboard
Copied
Use of functions can reduce the amount of code needed for an action. It is also used to perform recurring blocks of code. It can also make code readable by reducing the block of code to a single line. It also allows scripts to run more efficiently.
Copy link to clipboard
Copied
Not to be rude but I kind of already knew all of that. My struggle was that I couldn't get my for loop to work and the only way I could make work what I wanted to make work was by blowing up my loop into individual statement blocks. If you can rewrite what I've already figured out, I would more than willingly use it.
Copy link to clipboard
Copied
Did you look at the JavaScript console for errors?
I get a message about "True". This in not value of the logical true. It is all lower case, just like the logical false.
You have a number of statement that are not needed and I would test a value of the field against the field's defaultValue. An empty string might not always the default or cleared form value;
With some additional changes one can come up with:
// These are the required fields on the form.
// Populate array with their names.
var RequiredFields = new Array (
"Captain",
"Equipment",
"Date"
);
// These are the alert messages shown when a required field is empty.
// Populate array with messages.
// Make sure there's one message for each required field.
var alertMsg = new Array(
"Please enter the name of the Captain.",
"Please enter the Equipment ID.",
"Please enter the Date."
);
// var bSuccess = True // not correct True must be all lower case;
var fld = 0;
for (var i = 0; i < RequiredFields.length; i++ )
{
fld = this.getField(RequiredFields);
if(fld.value == fld.defaultValue)
{
app.alert(alertMsg);
fld.setFocus();
break;
} // end if;
} // end for loop;
if(i == RequiredFields.length) app.alert("success!", 3, 0); // or success action;
Copy link to clipboard
Copied
I had been struggling for quite some time to try to figure it out. Searching the internet wasn't coming up with any results of actual "fixes" to make it work. Thank you SOOO much for your input. That looks a lot more cleaner than mine. Thank you!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now