Skip to main content
Participating Frequently
November 14, 2023
Answered

Javascript - validate form issues

  • November 14, 2023
  • 3 replies
  • 1877 views

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);

    This topic has been closed for replies.
    Correct answer Nesa Nurani

    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.");

    3 replies

    Nesa Nurani
    Nesa NuraniCorrect answer
    Inspiring
    November 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.");
    N828Author
    Participating Frequently
    November 15, 2023

    It works!  Thanks!

    Bernd Alheit
    Adobe Expert
    November 14, 2023

    The following line makes no sense:

      if (!radioButton.value) {

    A radio button has always a value. 

    try67
    Adobe Expert
    November 14, 2023

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

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

    N828Author
    Participating Frequently
    November 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.

    kglad
    Adobe Expert
    November 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">
    N828Author
    Participating Frequently
    November 14, 2023

    Noted.  Thanks.

    kglad
    Adobe Expert
    November 14, 2023