I can't validate an array - help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dracenfels
    New Member
    • Aug 2007
    • 12

    I can't validate an array - help

    I've pulled together an online form that creates a number of tables dependant on the results of a dropdown list, i.e. 2 from the list creates two input tables, 10 creates 10 tables etc.

    The script for creating the tables is as follows;

    Code:
    <script type="text/javascript"><!--
    	function fboxes(NumberOfBoxes){	
    	var i,boxes='';
    	for(i=0; i<NumberOfBoxes; i++){	
    	boxes += "<table width='400' border='0' cellspacing='0' cellpadding='0' align='left'><tr><td><strong>Location</strong></td><td><input type='textarea' name='locbox["+i+"]' id='locbox["+i+"]' size='30'></td></tr><tr><td colspan='2'><div align='center'><strong>Incident Details</strong></div></td></tr><tr><td colspan='2'><textarea name='textbox["+i+"]' id='textbox["+i+"]' cols='35' rows='8'></textarea></td></tr><tr><td colspan='2'><div align='center'><strong>Business Impact</strong></div></td></tr><tr><td colspan='2'><textarea name='bimpbox["+i+"]' id='bimpbox["+i+"]' cols='35' rows='8'></textarea></td></tr></table>";
    	}
    	document.getElementById('currentissues').innerHTML = boxes;
    }
    
    --></script>
    This is fine and the php page that is actioned upon validation can see the information from all the various tables.
    What i need though is some validation for the boxes, namely, that if there is a table then the locbox[] field has to have text in it.

    Each time I've tried to write the validation part for this section it says that "locbox" isn't defined, yest it is (as in the script above).

    Any help at clearing this up and simplifying arrays would be greatly, muchly, enormously appreciated.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by nepomuk
    This would be JavaScript, wouldn't it? Ask in the JavaScript Forum! You know, Java isn't the same as JavaScript! ^^
    Moved to Javascript forum

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ...

      how did you try to retrieve the textbox-values? to retrieve it the right way you should use for example:

      [CODE=javascript]
      var box_value = document.getEle mentById('locbo x[1]').value;
      [/CODE]

      note: you cannot use locbox as an array or variable it is a simple string that is assigned to an elements id-attribute (respectivly its id-property too).

      kind regards

      Comment

      • Dracenfels
        New Member
        • Aug 2007
        • 12

        #4
        I tried this for the validation;

        Code:
           var total="";
            for(var i=0; i < locbox; i++){
            if(locbox[i].value =="") {
            total +=document.locbox[i].value + "\n";
            }
        	}
            if(total=="") {
            alert("fill something in");
            return false;
            }
        (I've put the whole form here http://www.eds.shed.googlepages.com/morningstatus.html if you want to take a look and it includes the wrong validation)

        Comment

        • Dracenfels
          New Member
          • Aug 2007
          • 12

          #5
          Originally posted by gits

          note: you cannot use locbox as an array or variable it is a simple string that is assigned to an elements id-attribute (respectivly its id-property too).

          kind regards
          I don't understand why this is as the php file which runs after validation understands all of the locbox[] values as an array, why can't another script on the first page do this?

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            hmmm ... i'm confused now. where do you want to validate your values ... serverside or clientside?

            Comment

            • Dracenfels
              New Member
              • Aug 2007
              • 12

              #7
              Originally posted by gits
              hmmm ... i'm confused now. where do you want to validate your values ... serverside or clientside?
              I'm validating the form clientside, hence the javascript, and using the php form to assemble the data and email it.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                ok ... in that case you don't have defined a variable locbox for javascript since php writes the names/ids to the document and it is a simple textual attribute of the input-nodes ... you have to retrieve the values of your textboxes with dom-methods ... like i showed you ...

                kind regards

                Comment

                • Dracenfels
                  New Member
                  • Aug 2007
                  • 12

                  #9
                  Originally posted by gits
                  ok ... in that case you don't have defined a variable locbox for javascript since php writes the names/ids to the document and it is a simple textual attribute of the input-nodes ... you have to retrieve the values of your textboxes with dom-methods ... like i showed you ...

                  kind regards
                  So would I have to write a var line for each of the possible box variables (possibly upto 10) like this:

                  Code:
                  var box_value0 = document.getElementById('locbox[0]').value;
                  var box_value1 = document.getElementById('locbox[1]').value;
                  etc
                  ?

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    nope ... you may do something like the following:

                    [CODE=javascript]var value_to_check;
                    var boxes = document.getEle mentsByTagName( 'input');

                    for (var i = 0; i < boxes.length; i++) {
                    var box = boxes[i];

                    if (/^locbox/.test(box.id)) {
                    value_to_check = box.value;
                    // check here the value_to_check ;) the way you want,
                    // or assign the values to an array or whatever
                    }
                    }
                    [/CODE]

                    kind regards

                    Comment

                    Working...