Skip to main content
Participant
April 29, 2019
Answered

Script not working check required fields before email submit

  • April 29, 2019
  • 1 reply
  • 665 views

Hi there,

I'm stuck on this annoying bit, pls halp!

I need this script to check required fields, and for a popup to appear and not allow the sending until all required fields are filled in.

I tried "else" but it won't work (it continues to send the email after anyway). I think I have something mixed up. I added "then" and it checked the required fields and didn't send the email but I cannot add the required field and submit the form via email, so that's not correct...

Could you help me?

var requiredFields = new Array();

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

{

var fName = this.getNthFieldName(i);

var f = this.getField(fName);

if (f.type!="button" && this.getField(fName).required && (this.getField(fName).value == null || this.getField(fName).value == ''))

{

requiredFields[requiredFields.length] = fName;}}

var error = "Please complete the following fields: \n\n";

for (j=0; j < requiredFields.length; j++)

{

if (requiredFields.value == null)

{

error = error + requiredFields + '\n';

var f2 = this.getField(requiredFields);

f2.setFocus();

}

 

}

 

if(requiredFields.length > 0)

app.alert(error);

//Main emails

var cToAddr = "meow@meow.com" ;

//Checkbox emails

var cb3 = this.getField("cb3").value; if (cb3!="Off") cToAddr += ";ff@ff.com";

var cb4 = this.getField("dfg").value; if (cb4!="Off") cToAddr += ";admin@blah.com";

var cb31 = this.getField("hhh).value; if (cb31!="Off") cToAddr += ";dbb@gvb.com";

var cb32 = this.getField("kkk").value; if (cb32!="Off") cToAddr += ";scheme@fgh.com";

//CC to persons

//CC to  other lists

var cCCAddr = "EHS@asd.com;" + "sdf@sdf.com;" +  + this.getField("SupervisorEmail").value;

//Email subject

var cSubLine = "New user registration form for " + this.getField("FirstName").value + " " + this.getField("LastName").value;

//Email message

var cBody = this.getField("FirstName").value + " " + "from the" + " " + this.getField("Workgroup").value + " workgroup has submitted their registration form.\n" + "Could you please add them to the email lists and organise access?\n" + "Thanks!"

//send email, attachment and message to recipients

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

This topic has been closed for replies.
Correct answer Jabberwocky1

nvm, just go it. I had the { } in the wrong spot!

1 reply

Jabberwocky1AuthorCorrect answer
Participant
April 29, 2019

nvm, just go it. I had the { } in the wrong spot!

try67
Community Expert
Community Expert
April 29, 2019

The code above will not work correctly with check-boxes. For it to work you need to change this part of it:

(this.getField(fName).value == null || this.getField(fName).value == '')

To:

this.getField(fName).valueAsString==this.getField(fName).defaultValue