View Single Post
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,168
Thanks: 59
Thanked 3,993 Times in 3,962 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 offline   Reply With Quote
Users who have thanked Old Pedant for this post:
helpmesoon (01-18-2013)