Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post How to set up an Array in javascript ?

    Hi,
    I'm new to the Forum world as well as a beginner in JavaScript. I'm working with a PDF Form in FOXIT Phantom PDF. My question; Some questions on the form have multiple entries. How do I set up an array to reduce the redundant coding that I have provide below. ... Thank You
    For Example;

    var txtFormErrors = this.getField("txtFormErrors");
    txtFormErrors.value = " ";
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //Question 4a
    var txtQ4aErrors = this.getField("txtQ4aErrors");
    txtQ4aErrors.textColor = color.red;
    txtQ4aErrors.value = "";

    var strSummaryQ4a = "";
    var strErrorQ4a = checkQ4a();

    if(strErrorQ4a == "")
    {
    txtQ4aErrors.value = "Question 4a: You have affirmed that the information is correct for the current reporting year. " ;
    txtQ4aErrors.textColor = color.green;

    strSummaryQ4a = "Q4a OK. \n" ;
    }
    else
    {
    txtQ4aErrors.value = strErrorQ4a;
    strSummaryQ4a = "Q4a Incomplete. \n";
    }
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    // Create the form validation summary
    txtFormErrors.value = (strSummaryQ4a);
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //**************************************************************************************************** **
    // Function to check Question 4a for errors
    function checkQ4a()
    {
    var strError = "";

    var fds_4anone = this.getField("fds_4anone");

    var fds_4aposition1 = this.getField("fds_4aposition1");
    var fds_4aorganization1 = this.getField("fds_4aorganization1");
    var fds_4aagency1 = this.getField("fds_4aagency1");

    var fds_4aposition2 = this.getField("fds_4aposition2");
    var fds_4aorganization2 = this.getField("fds_4aorganization2");
    var fds_4aagency2 = this.getField("fds_4aagency2");

    var fds_4aposition3 = this.getField("fds_4aposition3");
    var fds_4aorganization3 = this.getField("fds_4aorganization3");
    var fds_4aagency3 = this.getField("fds_4aagency3");

    var fds_4aposition4 = this.getField("fds_4aposition4");
    var fds_4aorganization4 = this.getField("fds_4aorganization4");
    var fds_4aagency4 = this.getField("fds_4aagency4");

    var fds_4aposition5 = this.getField("fds_4aposition5");
    var fds_4aorganization5 = this.getField("fds_4aorganization5");
    var fds_4aagency5 = this.getField("fds_4aagency5");


    if (fds_4anone.value != "Yes" &&
    (fds_4aposition1.value == "" && fds_4aorganization1.value == "" &&
    fds_4aposition2.value == "" && fds_4aorganization2.value == "" &&
    fds_4aposition3.value == "" && fds_4aorganization3.value == "" &&
    fds_4aposition4.value == "" && fds_4aorganization4.value == "" &&
    fds_4aposition5.value == "" && fds_4aorganization5.value == ""
    ))
    {
    strError = "You must check 'None' or fill in an answer.";
    }
    else if (fds_4anone.value == "Yes" &&
    (fds_4aposition1.value != "" || fds_4aorganization1.value != "" ||
    fds_4aposition2.value != "" || fds_4aorganization2.value != "" ||
    fds_4aposition3.value != "" || fds_4aorganization3.value != "" ||
    fds_4aposition4.value != "" || fds_4aorganization4.value != "" ||
    fds_4aposition5.value != "" || fds_4aorganization5.value != ""
    ))
    {
    strError = "All fields for this question must be left blank since you
    checked 'None' as your answer.";
    }
    else
    {
    if ((fds_4aposition1.value != "" || fds_4aorganization1.value != "" ) &&
    (fds_4aposition1.value == "" || fds_4aorganization1.value == "" ))
    {
    strError = "All fields in row 1 must be filled.\n";
    }

    if ((fds_4aposition2.value != "" || fds_4aorganization2.value != "" ) &&
    (fds_4aposition2.value == "" || fds_4aorganization2.value == "" ))
    {
    strError = strError + "All fields in row 2 must be filled in.\n";
    }

    if ((fds_4aposition3.value != "" || fds_4aorganization3.value != "" ) &&
    (fds_4aposition3.value == "" || fds_4aorganization3.value == "" ))
    {
    strError = strError + "All fields in row 3 must be filled in.\n";
    }

    if ((fds_4aposition4.value != "" || fds_4aorganization4.value != "" ) &&
    (fds_4aposition4.value == "" || fds_4aorganization4.value == "" ))
    {
    strError = strError + "All fields in row 4 must be filled in.\n";
    }

    if ((fds_4aposition5.value != "" || fds_4aorganization5.value != "" ) &&
    (fds_4aposition5.value == "" || fds_4aorganization5.value == "" ))
    {
    strError = strError + "All fields in row 5 must be filled in.\n";
    }
    }
    return strError;
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,889
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Ummm...you really only need one simple array. To keep track of which rows are blank/nonblank.

    Like this:
    Code:
    function isBlank( fname )
    {
        var val = this.getField( fname ).value;
        val = val.replace(/^\s+/,"").replace(/\s+$/,""); // trim off leading/trailing spaces
        return val == ""; // return true if field was blank
    }
    
    function checkQ4a()
    {
        var allOrNone = this.getField("fds_4anone").value;
        isYes = allOrNone == Yes"; // ???? THIS PROBABLY WON'T WORK!
    
        var allBlank = true;
        var rowBlank = [ ]; // array
        for ( var row = 1; row <= 5; ++row )
        {
            var threeblank = 
                   isBlank("fds_4aposition" + row)
                && isBlank("fds_4aorganization" + row)
                && isBlank("fds_4aagency" + row);
            allBlank &= threeblank;
            rowBlank[row] = threeblank;
        }
        if ( isYes )
        {
            if ( ! allBlank ) 
            {
                return "All fields for this question must be left blank since you checked 'None' as your answer.\n";
            }
            /* else: NONE and allBlank, so all is okay */
            return "";
        }
        // if we get here, NONE was *NOT* checked, so ...
        if ( allBlank ) 
        {
            return "You must check 'None' or fill in an answer.\n";
        } 
        var strError = "";
        for ( var row = 1; row <= 5; ++row )
        {
            if ( rowBlank[row] )
            {
                strError += "All fields in row " + row + "must be filled.\n";
            }
        }
        return strError;
    }
    If your field fds_4anone is a check box or a pair of radio buttons, then I don't think you can simply get its value like that. I think you would need to actually test to see what is checked/unchecked. But maybe not, since this is for PDF and not a browser. You will have to figure that part out.

    I do think that just testing for a blank/nonblank field is HORRIBLY inadequate. It means that a person could, for example, enter a single period (or single asterisk or ...) for each answer and you would accept it.

    Without knowing the kind of data you expect in all those fields, though, I can't do better than the blank test as you were doing it. I did improve it so that it strips off any leading or trailing spaces from the entered value. Which at least means that the user can't just enter a space for the answer.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    helpmesoon (01-18-2013)

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank You .... I'll incorporate your suggestion.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •