Skip to main content
talofaman
Participant
November 3, 2016
Question

Validate fields then lock

  • November 3, 2016
  • 2 replies
  • 477 views

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?

This topic has been closed for replies.

2 replies

talofaman
talofamanAuthor
Participant
November 12, 2016

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

  }

  }

  }

  }

}

}

Inspiring
November 12, 2016

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.

talofaman
talofamanAuthor
Participant
November 12, 2016

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.

talofaman
talofamanAuthor
Participant
November 4, 2016

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

}

}

}