www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Unable to validate row if any of the textbox values are entered

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    6

    resolved [RESOLVED] Unable to validate row if any of the textbox values are entered

    I am trying to validate row of textboxs. If any of the textbox values in that row are entered, then the user must enter all the textbox values in that row. It should also focus on the empty row. Else he should be able to submit the form. I tried the below code but it is looks like that there is a logic issue. Please help me


    PHP Code:
    <script>
    function 
    checkVal()
    {
        var 
    counter 0;
        var 
    a  =  document.getElementById('name'+counter).value;
        
    alert(a);
        for(var 
    i=0i<10i+5)
            {
     if(
    a.length>1)
         {
         var 
    document.getElementById('name'+counter).value;
         for(var 
    k=0k<5k++)
             {
             if(
    k[i].length<0)
                 {
                 
    alert("Enter Values for all rows.");
                 
    k[i].focus();
                 return 
    false;
                 }
             return 
    true;
             }
         }
            }
        }
    </script>
    <form name="na"  onsubmit="return checkVal()">
    <table>
          <tr>
      <td><input type="text" name="s1" id="s1" value="1" size="2" readonly="readonly"/></td>

    <td><input type="text" name="name0" id="name0" size="20"> </td>
     <td><input type="text" name="name1" id="name1" size="20"> </td>
     <td><input type="text" name="name2" id="name2" size="20"> </td>
     <td><input type="text" name="name3" id="name3" size="15">  
     <td><input type="text" name="name4" id="name4" size="15">  
     </tr>
      <tr>
     <td><input type="text" name="s1" id="s2" size="2" value="2" readonly="readonly"/></td>
     <td><input type="text" name="name0" id="name5" size="20"> </td>
     <td><input type="text" name="name1" id="name6" size="20"> </td>
     <td><input type="text" name="name2" id="name7" size="20"> </td>
     <td><input type="text" name="name3" id="name8" size="15">  
     <td><input type="text" name="name4" id="name9" size="15"> 
     </tr>
      <tr>
      <td><input type="text" name="s1" id="s3" size="2" value="3" readonly="readonly"/></td>
     <td><input type="text" name="name0" id="name10" size="20"> </td> 
     <td><input type="text" name="name1" id="name11" size="20"> </td>
     <td><input type="text" name="name2" id="name12" size="20"> </td>
     <td><input type="text" name="name3" id="name13" size="15">  
     <td><input type="text" name="name4" id="name14" size="15"> 
     </tr>
    </table>

    <input type="submit" value="Submit">
    </form>
    </body> 

  2. #2
    Join Date
    Jul 2011
    Posts
    127
    You have an infinite loop here
    Code:
    for(var i=0; i<10; i+5)
    and some other logic issues.

    Try this code. It should work:
    Code:
    function checkVal() 
    { 
        var rows = document.forms[0].getElementsByTagName('table')[0].rows,
    		rows_count = rows.length,
    		inputs_per_row = 5,
    		i = 0,
    		input, j, is_validated_row;
    
    	for (; i < rows_count; i++) {
    		is_validated_row = false;
    		// Check if any input has a value.
    		for (j = 0; j < inputs_per_row; j++) {
    			if (document.getElementById('name' + (i * inputs_per_row + j)).value !== '') {
    				is_validated_row = true;
    				break;
    			}
    		}
    		// If any input in the row has a value then validate all other inputs for the row.
    		if (is_validated_row === true) {
    			for (j = 0; j < inputs_per_row; j++) {
    				input = document.getElementById('name' + (i * inputs_per_row + j))
    				if (input.value === '') {
    					input.focus();
    					alert('Enter Values for all rows.');
    					return false;
    				}
    			}
    		}
    	}
    
    	return true;
    }

  3. #3
    Join Date
    Jul 2013
    Posts
    6
    Thank you very much... The issue is resolved.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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