www.webdeveloper.com
Results 1 to 5 of 5

Thread: Please explain some code

  1. #1
    Join Date
    Mar 2009
    Posts
    41

    Please explain some code

    Hi,

    I do not understand some code which I have used. I can see what it is doing and have implemented it successfully but cannot seem to validate it.

    I have a form where by a course can be added. On this form I have a button which will allow the user to add levels dynamically as many as 10. This is to save time so that they dont have to input the same data over and over again for the various levels. This works fine and I am successfully writing to the database. The problem is that I want to check all of the level fields that have been added to make sure that they are not left blank.

    The code is as follows:

    Code:
    // This function will dynamically add level fields to the form.
    var counter = 1;
    var limit = 10;
    function addInput(divName)
    {
         if (counter == limit)  
         {
              alert("You have reached the limit of adding " + counter + " inputs");
         }
         else 
         {
              var newdiv = document.createElement('div');
              newdiv.innerHTML = "<label>Level"+ (counter)+ ": *</label><input type='text' name='levels[]'>"; 
              document.getElementById(divName).appendChild(newdiv);
              counter++;
         }
    }
    
    // The form button to add a new level calling above function
    <div id="dynamicInput">
    				      <input type="button" value="Add level" onClick="addInput('dynamicInput');">
    				  </div>
    Any advice would be much appreciated so that I can validate all of the fields making sure they are not NULL.

    Thanks in advance

  2. #2
    Join Date
    Mar 2009
    Posts
    41
    Ok I have made some progress..

    My idea is that I loop through using a for loop as I know how many levels have been added and try and use the GetElementByID to find the field and then check it for NULL.

    This doesnt seem to work though.

    My code is as follows:

    Code:
    newdiv.innerHTML = "<label>Level"+ (counter)+ ": *</label><input type='text' id='level'+counter name='levels[]'>"; 
              document.getElementById(divName).appendChild(newdiv);
    
    
    // Validate the discount fields
    	function validateValue()
    	{
    		var i = 1;
    		for ( i; i <= counter-1; i++ )
    		{
    			var level = 'level' +i;
    			if( document.getElementById(level).value == '' );
    			{
    				alert( "Cannot be left blank" );
    			}
    		}
            }
    This does not seem to work properly I suspect that there is a problem with the form bit.
    newdiv.innerHTML = "<label>Level"+ (counter)+ ": *</label><input type='text' id='level'+counter name='levels[]'>";

    But am not sure

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Get an array of the name
    Code:
    var levels = document.formName.elements['levels[]'];
    for(var i = 0; i < levels .length; i++) {
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    Mar 2009
    Posts
    41

    resolved

    I see what you mean that is a much better way of doing it!

    It doesn't seem to like the for loop though?

    Code:
    for( var i = 0; i < levels.length; i++ ) 
    		{
    			alert( "" );	
    		}
    Also when this will compile should I just be able to simply check each value for NULL??

    Maybe something like this.

    if ( levels[i] == "" )
    {
    Do something
    }

    Thank you once again and sorry for late reply I have limited access to the Internet at the moment.

    Joe

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    for( var i = 0; i < levels.length; i++ ) 
    {
    if(levels[i].value=='') {alert( "Cannot be left blank" );}
    }
    At least 98% of internet users' DNA is identical to that of chimpanzees

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles