Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-17-2013, 07:32 PM   PM User | #1
helpmesoon
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
helpmesoon is an unknown quantity at this point
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;
}
helpmesoon is offline   Reply With Quote
Old 01-17-2013, 11:33 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
helpmesoon (01-18-2013)
Old 01-18-2013, 06:19 PM   PM User | #3
helpmesoon
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
helpmesoon is an unknown quantity at this point
Thank You .... I'll incorporate your suggestion.
helpmesoon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:52 AM.


Advertisement
Log in to turn off these ads.