How to require fields to be filled in when using this.mailDoc

I have a this.mailDoc script in a form I'm putting together. It's working fine, but what I'm needing are instructions on how to require certain fields to be filled in before the this.mailDoc can execute. I know how to do this using the stock Submit Form button, but that won't work for me in my particular situation. Thoughts?


Gregory Talkington


2 Answers

When you use a "normal" submit button, the form will use the "required" flag that you can assign to form fields in order to figure out if the form is ready to be submitted. When using the Doc.mailDoc function, you need to do this yourself.

One way I've done this in the past is to have a loop that goes through all form fields and looks for the "required" property (http://livedocs.adobe.com/acrobat_sdk/11/Acrobat11_HTMLHelp/JS_API_AcroJS.89.726.html). When it finds a field that is required, it then checks to see if the value is different from the default value - or different from an empty string. It's up to you to decide what constitutes a valid entry in a field.

If you find one or more required fields that have no valid data, you would then pop up an alert, and not call the mailDoc function. If everything is OK, the document would be mailed.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

Actually, if you've already marked the fields as required, then there is an easier way: You can loop over all fields and then just verify that all fields that have the required flag set have a valid value:

var is_form_ok = true;
for (var i=0; i<this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f.type == "text" && f.required) {
        if (f.value == 0) {
            is_form_ok = false;
            i = this.numFields; // stop the loop
        }
    }
}

if (is_form_ok) {
    // all required fields contain data
    app.alert("OK");
}
else {
    app.alert("Please fill in all required fields");
}

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   


Please specify a reason: