View Full Version : How to stop a Javascript

03-22-2012, 08:30 PM
Hi Guys,

Quick question, is there a way to stop a JavaScript from validating after a certain point of the code. I've been "googling" like crazy and cant find a solution.

I had to add a Checkbox validation last second and there was existing validation and I'm not good enough to alter all of it to reflect the checkbox part so what I did was I checked to see if at least one of the checkboxes was selected, if yes, do nothing and finish the rest of the validation. If no, prompt them with an error.

The problem is after it prompts them for an error, it continues to run the rest of the validation and at the end it allows the end user to print. I cannot allow the end user to print until everything is filled in. Here is my code:

function validateFields() {
var fieldsToVerify = [


var emptyFields = [];
var notFullFields = [];
for (var i=0; i<fieldsToVerify.length; i++) {
var f = this.getField(fieldsToVerify[i][0]);
if (f==null) {
console.println(fieldsToVerify[i][0] + " not found.");
if (f.valueAsString=="") {
} else if (fieldsToVerify[i].length>1 && f.valueAsString.length<fieldsToVerify[i][1]) {

for (var i in nonRequiredFieldsToVerify) {
var f = this.getField(nonRequiredFieldsToVerify[i]);
if (f==null) {
console.println(nonRequiredFieldsToVerify[i] + " not found.");
if (f.valueAsString!="" && f.valueAsString.length<f.charLimit) {

(this.getField("CheckBox1").value=="Yes" || this.getField("CheckBox2").value=="Yes" || this.getField("CheckBox3").value=="Yes" || this.getField("CheckBox4").value=="Yes" || this.getField("CheckBox5").value=="Yes" || this.getField("CheckBox6").value=="Yes" || this.getField("CheckBox7").value=="Yes")



app.alert("Select a Checkbox in Section I before printing")

if (emptyFields.length==0 && notFullFields.length==0) {
} else {
var msg = "";
if (emptyFields.length>0) {
msg += "The following fields must be filled-in:\n";
for (var i in emptyFields)
msg += emptyFields[i]+"\n";

if (notFullFields.length>0) {
if (msg!="") msg+="\n\n";
msg += "The following fields are not filled-in completely:\n";
for (var i in notFullFields)
msg += notFullFields[i][0] + " (required length: " + notFullFields[i][1] + ")\n";

03-22-2012, 09:11 PM
Id like to stop it after the part in the red until they select a checkbk and click the print button again...is this possible?

03-23-2012, 08:36 AM
you can explicitly return from the function at that (or any otherplace).

03-23-2012, 08:54 AM
If you want it to stop a form from being submitted then you need to return false from iside your function and also return the function to the onsubmit handler.

03-26-2012, 03:23 PM
So I added the "return" after my error message for not selecting the checkbox and it prevents the rest of the script from running (perfect!)

The problem is when I do select one of the checkboxes and hit print, the button does nothing.

I think this was what felgall was hinting at but I'm not sure what the onsubmit handler is??

Or I could be completely wrong but is there a way to get my script to validate from the beginning once I have made the change and hit the print button again?