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

Javascript - validate form issues

Community Beginner ,
Nov 14, 2023 Nov 14, 2023

I am tyring to create a button using javascript that will do the following:

 

There are 3 signature lines - PO, SPO, Chief.  I want the PO line hidden until the button is click and the form is completed. 

 

  • Check to make sure the form is complete
    • All text fields
    • All radio buttons
    • Excluding SPO and Chief signature lines 
    • If the form is not complete - display an error message and what needs completed.
  • If the form is complete 
    • The CHECK FORM button will disappear
    • The PO signature line will appear

 

I've used different lines of code so far with mixed results.  I can get parts of it work but not all of it.  Once I fix something, there's another issue.  Below is the last line of code I was trying to use.

 

Code is listed below - Any insight is appreciated:

 

var poSignatureField = this.getField("PO");

var checkFormButton = this.getField("CHECK FORM");

 

function validateTextFields() {

    var textFields = ["Date5_af_date", "Name", "PACTS", "Q1", "Q2", "3text", "4text", "5text", "6text", "10text", "11text", "12text"];

    for (var i = 0; i < textFields.length; i++) {

        var field = this.getField(textFields[i]);

        if (field.value === "" && field.name !== "SPO" && field.name !== "Chief") {

            return false;

        }

    }

    return true;

}

 

function validateRadioButtons() {

    var radioButtons = ["Q3", "Q4", "Q6", "Q7", "Q8", "Q9", "Q10"];

    for (var i = 0; i < radioButtons.length; i++) {

        var radioButton = this.getField(radioButtons[i]);

        if (!radioButton.value) {

            return false;

        }

    }

    return true;

}

 

function checkFormButtonClick() {

    if (validateTextFields() && validateRadioButtons()) {

        checkFormButton.display = display.hidden;

        poSignatureField.display = display.visible;

    } else {

        app.alert("Please complete all required fields before submitting the form.");

    }

}

 

checkFormButton.setAction("MouseUp", checkFormButtonClick);

2.0K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Nov 14, 2023 Nov 14, 2023

Try this:

var poSignatureField = this.getField("PO");
var tf = false, rb = false;
var textFields = ["Date5_af_date", "Name", "PACTS", "Q1", "Q2", "3text", "4text", "5text", "6text", "10text", "11text", "12text"];
var radioButtons = ["Q3", "Q4", "Q6", "Q7", "Q8", "Q9", "Q10"];

for (var i in textFields) {
 if(this.getField(textFields[i]).valueAsString === "")
  tf = true;}

for (var j in radioButtons){
 if(this.getField(radioButtons[j]).valueAsString === "Off")
  rb = true;}

if(tf == false && rb == false){
 event.target.display = display.hidden;
 poSignatureField.display = display.visible;}
else
 app.alert("Please complete all required fields before submitting the form.");

View solution in original post

Translate
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 ,
Nov 14, 2023 Nov 14, 2023

in the future, to find the best place to post your message, use the list here, https://community.adobe.com/

p.s. i don't think the adobe website, and forums in particular, are easy to navigate, so don't spend a lot of time searching that forum list. do your best and we'll move the post (like this one has already been moved) if it helps you get responses.



<"moved from using the community">
Translate
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 Beginner ,
Nov 14, 2023 Nov 14, 2023

Noted.  Thanks.

Translate
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 ,
Nov 14, 2023 Nov 14, 2023
Translate
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 ,
Nov 14, 2023 Nov 14, 2023

The following line makes no sense:

  if (!radioButton.value) {

A radio button has always a value. 

Translate
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 ,
Nov 14, 2023 Nov 14, 2023

Nor this part, since those names don't appear in the array in the first place:

&& field.name !== "SPO" && field.name !== "Chief"

Translate
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 Beginner ,
Nov 15, 2023 Nov 15, 2023

I should have mentioned this was my first time really trying javascript.   I looked at a lot of adobe forums and tried  different things.  I also only discovered this morning that I had to enable the javascript debugger in adobe.  Which I was not aware it existed until I was watching a video last night on youtube.

Translate
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 ,
Nov 14, 2023 Nov 14, 2023

Try this:

var poSignatureField = this.getField("PO");
var tf = false, rb = false;
var textFields = ["Date5_af_date", "Name", "PACTS", "Q1", "Q2", "3text", "4text", "5text", "6text", "10text", "11text", "12text"];
var radioButtons = ["Q3", "Q4", "Q6", "Q7", "Q8", "Q9", "Q10"];

for (var i in textFields) {
 if(this.getField(textFields[i]).valueAsString === "")
  tf = true;}

for (var j in radioButtons){
 if(this.getField(radioButtons[j]).valueAsString === "Off")
  rb = true;}

if(tf == false && rb == false){
 event.target.display = display.hidden;
 poSignatureField.display = display.visible;}
else
 app.alert("Please complete all required fields before submitting the form.");
Translate
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 Beginner ,
Nov 15, 2023 Nov 15, 2023
LATEST

It works!  Thanks!

Translate
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