Skip to main content
Participant
August 1, 2018
Answered

Validating Fields in Javascript while preventing an email to be sent until all required fields are completed.

  • August 1, 2018
  • 1 reply
  • 3773 views

Hi,

I have a form with a set of required fields that upon clicking submit an email is generated with the certain fields populating the email body. I want it so that the email won't be generated until all the required fields have been filled in.

The script I put together identifies the required fields, shows a notification to the user that they aren't completed, and highlights them in red when you click submit, but it also generates the email after the pop-up appears that fields need to be completed. Is there a way to prevent that from happening until all the required fields are filled in? I have been going through these forums and have been taking snippets of code from a variety of places so I have totally missed something, but any help is greatly appreciated.

Here is my code currently.

\\Validation of the fields

var emptyFields = new Array(); // array to hold incomplete field names;

var oField; // variable to hold field object being testee;

var bProcess =  true; // assume all fields completed;

for (var i = 0; i < this.numFields; i++) {

      oField = this.getField(this.getNthFieldName(i));

     if (oField.type!="button" && oField.required ) {

     // check based on field type

  switch(oField.type) {

  case "button":

  // do nothing;

  break;

  case "checkbox":

  case "radiobutton":

   if(oField.value == oField.defaultValue && oField.value == "Off") {

   emptyFields.push(f.name);

   bProcess = false;

   }

  break;

  case "combobox":

  case "listbox":

  case "text": 

   if(oField.value == oField.defaultValue) {

   emptyFields.push(oField.name);

    bProcess = false;

    }

  break; 

  case "signature":

  if(oField.value == oField.defaultValue) {

   emptyFields.push(f.name);

   bProcess = false;

  }

  break;

  default:

  app.alert("Field not processed " + oField.name + " type " + oField.type, 1, 0);

   bProcess = false;

  break;

  } // end switch type;

  } // end if required;

} // end loop of fields;

if (bProcess == true) {

     // do something;

  app.alert("all fields completed", 3, 0);

} else {

app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"), 1, 0);

}

// Setting up the email to send

var cToAddr = "lihelp@phila.gov"

// CC Email Address

var cCCAddr = "pfd.fcu@phila.gov"

// Set the subject and body text for the email message

var cSubLine = "Dangerous Property Referral -" + " " + this.getField("Address").value;

var cBody = "Please review the information about the following property:" + '\r\n' + '\r\n'+

"Address:" + " " + this.getField("Address").value + '\r\n' +

"Occupancy Type:" + " " + this.getField("Occupancy Type").value + '\r\n' +

"Building Type:" + " " + this.getField("Building Type").value + '\r\n' +

"Construction Type:" + " " + this.getField("Construction Type").value + '\r\n' +

"Heavy Contents:" + " " + this.getField("Heavy Contents").value + '\r\n' +

"HazMats:" + " " + this.getField("HazMats - yes/no").value + '\r\n' +

"Prior Fire:" + " " + this.getField("Prior fire - yes/no").value  + '\r\n' +

"Known Hazards:" + " " + this.getField("Known hazards - Details").value + '\r\n' +

"Structural Concerns:" + " " + this.getField("Structural Concerns - Details").value;

// Send the entire PDF as a file attachment on an email

this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});

This topic has been closed for replies.
Correct answer try67

Thanks for the response.

I am not entirely sure how I should go about changing the code. I have pretty much just tried to amend all of your contributions that I have seen in these forums so they fit my data. Not sure how to go about simplifying it the validation.

At least it's pretty much working except for that dropdown box I referenced above that isn't registering as having been filled in.

Thanks again.


Replace all of this:

    // check based on field type

  switch(oField.type) {

  case "button":

  // do nothing;

  break;

  case "checkbox":

  case "radiobutton":

   if(oField.value == oField.defaultValue && oField.value == "Off") {

   emptyFields.push(f.name);

   bProcess = false;

   }

  break;

  case "combobox":

  case "listbox":

  case "text": 

   if(oField.value == oField.defaultValue) {

   emptyFields.push(oField.name);

    bProcess = false;

    }

  break; 

  case "signature":

  if(oField.value == oField.defaultValue) {

   emptyFields.push(f.name);

   bProcess = false;

  }

  break;

  default:

  app.alert("Field not processed " + oField.name + " type " + oField.type, 1, 0);

   bProcess = false;

  break;

  } // end switch type;

With just this:

if (oField.valueAsString == oField.defaultValue) {

   emptyFields.push(oField.name);

    bProcess = false;

}

1 reply

try67
Community Expert
Community Expert
August 1, 2018

Move the part of the code that emails the file next to where it says "// do something;"...

Participant
August 1, 2018

Thank you!

Two more questions.

Your fix appears to work, with the exception of 1 dropdown field. It's a dropdown field that has a blank as the default and then a Yes or a No option for the question if there is a Basement or Cellar. I have selected No and it still gives me the pop-up that it's not filled in.

Also how would I go about highlighting the fields that were required that were not filled in so the user knows what they need to complete?

Thank you so much for your help!

Participant
August 1, 2018

Here is my updated code with your original suggested change, just for reference:

var emptyFields = new Array(); // array to hold incomplete field names;

var oField; // variable to hold field object being testee;

var bProcess =  true; // assume all fields completed;

for (var i = 0; i < this.numFields; i++) {

      oField = this.getField(this.getNthFieldName(i));

     if (oField.type!="button" && oField.required ) {

     // check based on field type

  switch(oField.type) {

  case "button":

  // do nothing;

  break;

  case "checkbox":

  case "radiobutton":

   if(oField.value == oField.defaultValue && oField.value == "Off") {

   emptyFields.push(f.name);

   bProcess = false;

   }

  break;

  case "combobox":

  case "listbox":

  case "text": 

   if(oField.value == oField.defaultValue) {

   emptyFields.push(oField.name);

    bProcess = false;

    }

  break; 

  case "signature":

  if(oField.value == oField.defaultValue) {

   emptyFields.push(f.name);

   bProcess = false;

  }

  break;

  default:

  app.alert("Field not processed " + oField.name + " type " + oField.type, 1, 0);

   bProcess = false;

  break;

  } // end switch type;

  } // end if required;

} // end loop of fields;

if (bProcess == true) {

// do something;

// This is the form return email.

var cToAddr = "lihelp@phila.gov"

// First, get the client CC email address

var cCCAddr = "pfd.fcu@phila.gov"

// Set the subject and body text for the email message

var cSubLine = "Dangerous Property Referral -" + " " + this.getField("Address").value;

var cBody = "Please review the information about the following property:" + '\r\n' + '\r\n'+

"Address:" + " " + this.getField("Address").value + '\r\n' +

"Occupancy Type:" + " " + this.getField("Occupancy Type").value + '\r\n' +

"Building Type:" + " " + this.getField("Building Type").value + '\r\n' +

"Construction Type:" + " " + this.getField("Construction Type").value + '\r\n' +

"Heavy Contents:" + " " + this.getField("Heavy Contents").value + '\r\n' +

"HazMats:" + " " + this.getField("HazMats - yes/no").value + '\r\n' +

"Prior Fire:" + " " + this.getField("Prior fire - yes/no").value  + '\r\n' +

"Known Hazards:" + " " + this.getField("Known hazards - Details").value + '\r\n' +

"Structural Concerns:" + " " + this.getField("Structural Concerns - Details").value;

// Send the entire PDF as a file attachment on an email

this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});

  app.alert("all fields completed", 3, 0);

} else {

app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"), 1, 0);

}