Skip to main content
Participant
July 17, 2018
Answered

Need help with my VerifyField JavaScript

  • July 17, 2018
  • 1 reply
  • 1195 views

Hello,

I've created a form in which certain check boxes and fields are required. When clicking on the submit button, I would like it to verify if those check boxes and fields have been filled in. If they have, then send a mail with the PDF. However, if they haven't, then highlight the empty fields.

Unfortunately, I know pretty much nothing of JavaScript, which means that if I find an article that shows the solution, it doesn't work for me (probably because I didn't specify the names of the fields...). Can you please help me out?

The check boxes that need to be filled in are "Customer" and "Site". The fields that are required are "Name" and "Contact".

This topic has been closed for replies.
Correct answer try67

Highlighting the fields is a bit tricky. Try this code first:

if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});

function validateRequiredFields(doc) {

    var emptyFields = [];

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

        var f = doc.getField(doc.getNthFieldName(i));

        if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {

            emptyFields.push(f.name);

        }

    }

    if (emptyFields.length>0) {

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

        return false;

    }

    return true;

}

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 17, 2018

Highlighting the fields is a bit tricky. Try this code first:

if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});

function validateRequiredFields(doc) {

    var emptyFields = [];

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

        var f = doc.getField(doc.getNthFieldName(i));

        if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {

            emptyFields.push(f.name);

        }

    }

    if (emptyFields.length>0) {

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

        return false;

    }

    return true;

}

laulzAuthor
Participant
July 17, 2018

It doesn't have to highlight, it can just be a red box or a pop-up or basically just something that indicates that values are missing.

Unfortunately, that didn't work. Nothing happens if I leave the fields blank or if I fill them in (at which point I should be receiving a mail I guess).

I changed the email address, changed the name of the button and changed the name of the field. First I tried by using the names of 2 different fields and then just the one "Name" field.

try67
Community Expert
Community Expert
July 17, 2018

Don't change anything in the code I provided, especially if you don't know what you're doing...