• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

JavaScript Validation to Submit Form

New Here ,
Oct 09, 2008 Oct 09, 2008

Copy link to clipboard

Copied

Hi,

How do you validate all the fields in JavaScript when the user clicks the Submit button so that if any required fields are empty or any field validation fails, nothing (submission, JavaScript code, execute menu items, etc.) will be executed? I found something about execValidate() but it didnt work for me. It wouldnt do anything. I am using Adobe Acrobat Professional 8.0.

For instance, I have a field set up to accept numbers between 1111 and 9999. If the user enters 1 and tabs out of the field, he gets an error message and the value is deleted. The field is also set as a required field. But if the user clicks the Submit button, all JavaScript code will be executed, menu items too. The only thing that fails is "Submit a Form" only for the required field part. What can I include in the JavaScript code to check if all fields are valid, according to their corresponding rules, and if yes, continue to execute all the other actions. If not, cancel all and let user make the necessary changes.

Thank you for your help!

Views

28.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2008 Oct 09, 2008

Copy link to clipboard

Copied

The "Required" property works in conjunction with a "submit" to a web server script page. If you want to use this feature for check "required" fields before submitting, you must write the JavaScirpt to check the specific fields or all the fields that support the "requried' property and determine if they have been completed as necessary.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 17, 2009 Feb 17, 2009

Copy link to clipboard

Copied

I would like further information on the same point. I have built a form with "required" fields. By default, when the submit button is pushed, a message pops up preventing the form from being submitted because the "required" fields are empty. However, in the same mouse click button I am also endeavoring to make all the fields "Read Only" so that when the document is processed, the values are not easily changed (submission is as a PDF, not FDF).

I basically need to be able to not mark a required field as Read Only if it contains no data, but I'm having trouble figuring out the code. Here is what I have so far...

On my button "Mouse Up" Event there are two "Actions":
- Run a Java Script
- Submit a form

The Java script is very simple...

I basically need to understand the code for checking the values before marking them read only.

I tried to build a check of just one field first (since I have a total of 10 to check) thinking if I get one right, then I can modify it for the rest. I tried an IF statement, but my syntax is wrong. Please help.

var S = this.getField("TR.Name").value;
If (S!="") {
app.alert("You must fill in all required fields before submitting.");
} else
{var T = this.getField("TR");
var F = this.getField("FD");
T.readonly = true;
F.readonly = true;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

just learning here:

What does the "DR" and "FR" represent in this code? Are they field names?

else

{

var T = this.getField("DR");

var F = this.getField("FD");

T.readonly = true;

F.readonly = true;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

LATEST

This are field names.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 17, 2009 Feb 17, 2009

Copy link to clipboard

Copied

Change the comparison operator from != to ==, and field name TR to something else and you should be fine.

var S = this.getField("TR.Name").value;

if (S =="")
{
app.alert("You must fill in all required fields before submitting.");
T.readonly = false;
F.readonly = false;
}

else
{
var T = this.getField("DR");
var F = this.getField("FD");
T.readonly = true;
F.readonly = true;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2009 Mar 04, 2009

Copy link to clipboard

Copied

(Sorry in advance - I'm new at this)
I'm using a button with two actions - the javascript above and submit a form. However, when the validation fails (the required field is not filled in), the user simply clicks "ok" and then is able to submit the form anyway. Is there a way to prevent the user from submitting the form when the validation fails?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 04, 2009 Mar 04, 2009

Copy link to clipboard

Copied

The button's actions are independent so if you want a condition to be meet before submission, you must do all of that within one complete block of JavaScirpt code action and not 2.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2009 Mar 04, 2009

Copy link to clipboard

Copied

Hi Geo - thanks, but I'm still confused. I'm in the same boat as Jason above: On my button "Mouse Up" Event there are two "Actions":
- Run a Java Script
- Submit a form

This is what I have now for javascript:

var S = this.getField("Signature").value;

if (S =="Off")
{
app.alert("You must check the signature box before submitting.");
T.readonly = false;
F.readonly = false;
}

Are you saying that my submit action needs to be written into the javascript above? Can I get a sample somewhere?

else
{
var T = this.getField("DR");
var F = this.getField("FD");
T.readonly = true;
F.readonly = true;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2009 Mar 04, 2009

Copy link to clipboard

Copied

Sorry - that got clipped. Are you saying that I need to write the submit action out in javascript in the validation action above? Is there a sample somewhere that I can use?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 04, 2009 Mar 04, 2009

Copy link to clipboard

Copied

The two actions are independent, so if the first action sets anything or fails the second action will have no knowledge of that situation and the second action will be executed.

There is information about the "submitForm()" method in the Acrobat JavaScript API, which is part of the SDK.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 29, 2010 Jan 29, 2010

Copy link to clipboard

Copied

Hello Everyone,

I like this script and was hoping I could get some help.  I would just like to incorporate multiple required fields.  I'm no export programmer (I'm sure its obvious by looking by example).  Any suggestions!?

var S = this.getField("test1").value;

var Q = this.getField("test2").value;

if (S =="") and (Q =="")

{

app.alert("You must fill in all required fields before submitting.");

T.readonly = false;

F.readonly = false;

}

else

{

var T = this.getField("DR");

var F = this.getField("FD");

T.readonly = true;

F.readonly = true;

}

Thank you,

CJ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2010 Jan 29, 2010

Copy link to clipboard

Copied

Problem 1:

Change this line:

if (S =="") and (Q =="")

To this:

if ((S =="") && (Q ==""))

Problem 2:

You are trying to access T and F in the first block without declaring them.

I suggest you declare them at the beginning of the script, after S and Q.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2010 Jan 29, 2010

Copy link to clipboard

Copied

(also, you should open a new thread, not hijack this one)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 29, 2010 Jan 29, 2010

Copy link to clipboard

Copied

Wow, thank you for your quick reply.  I'll give you what you suggested a shot.

Also thanks for the tip on forum etiquette (still new)...sorry.

Thank you,

CJ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 20, 2011 Jan 20, 2011

Copy link to clipboard

Copied

anybody know if is there any way of getting an arrayList with the whole Fields in the form? It would be great if I we could going through the structure in the same way we can do it in Javascript and HTML with "getElementsByTagName" instead of creating an array of Strings with all the fields' names and doing this.getField("...") for each one

Thanks a lot

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2011 Jan 20, 2011

Copy link to clipboard

Copied

Look at the method getNthFieldName in conjunction with the numFields

property of the Document object.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 20, 2011 Jan 20, 2011

Copy link to clipboard

Copied

ok thank you a lot, it seems works fine 😉


This is my code:

//this part is for validate all the fields in document by sections

//Validation methods throws alerts in case of error and focus the cursor on the wrong field

var okres=validarSolicitante();
if (okres=="ok"){
    okres="";
    okres=validarOtrosDatos();
}
if (okres=="ok"){
    okres="";
    okres=validarFirma();
}

//After validation, if is all is correct we show a confirm message and proceed to submit the form
if (okres=="ok"){
    var nButton = app.alert({cMsg: "¿Está seguro de que desea enviar los datos del formulario?",
                             cTitle: "Confirmación de envío", nIcon: 2, nType: 2});

    if ( nButton == 4 ) {
        var campos = new Array();
        var f;
        var pos = 0;
        //app.alert("nCampos del formulario:" + this.numFields);
       
        //we collect all the fields into an array
        for ( var i=0; i < this.numFields; i++) {
            var fname = this.getNthFieldName(i);
            f = this.getField(fname);
            if ( f.type != "button" ){
                campos[pos] = fname;
                pos++;
            }
        }

        /*we can show with alerts that all is ok
        app.alert("Array de campos:" + campos.length);
        for ( var i=0; i < campos.length ; i++) {
            f = this.getField(campos);
            app.alert("(" + i + ") " + campos + " = " + f.value);
        }
        */


        //and submit the form

        this.submitForm({cURL: "https://www...", bEmpty: true, aFields: campos, cSubmitAs: "FDF"});
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines