Validating Fields in Javascript while preventing an email to be sent until all required fields are completed.
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});
