Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question validate row if any of the textbox values are entered in that row

    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
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,114
    Thanks
    75
    Thanked 4,336 Times in 4,302 Posts
    It can be made to work, as is. But I'd suggest that you change your <form> field names to make it more consistent/usable.

    Oh, and GET RID of the IDs. Unless you are using <label>s, they are a waste of coding for <form> fields.

    Code:
    <tr> 
     <td><input type="text" name="s1" value="1" size="2" readonly="readonly"/>/td> 
     <td><input type="text" name="name1_0"  size="20"> </td> 
     <td><input type="text" name="name1_1" size="20"> </td> 
     <td><input type="text" name="name1_2" size="20"> </td> 
     <td><input type="text" name="name1_3" size="15">   
     <td><input type="text" name="name1_4" size="15">   
    </tr> 
    <tr> 
     <td><input type="text" name="s2" value="1" size="2" readonly="readonly"/>/td> 
     <td><input type="text" name="name2_0"  size="20"> </td> 
     <td><input type="text" name="name2_1" size="20"> </td> 
     <td><input type="text" name="name2_2" size="20"> </td> 
     <td><input type="text" name="name2_3" size="15">   
     <td><input type="text" name="name2_4" size="15">   
    </tr>
    You see that? Now all the entries in a single row are linked by their names.
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,114
    Thanks
    75
    Thanked 4,336 Times in 4,302 Posts
    One way to do it, then:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form id="na"><!-- named forms are OBSOLETE...use an id! -->
    <table border="1" cellpadding="3">
    <tr> 
        <td><input type="text" name="s1" value="1" size="2" readonly="readonly"/></td> 
        <td><input type="text" name="name1_0"  size="20"/></td> 
        <td><input type="text" name="name1_1" size="20"/></td> 
        <td><input type="text" name="name1_2" size="20"/></td> 
        <td><input type="text" name="name1_3" size="15"/></td>    
        <td><input type="text" name="name1_4" size="15"/></td>    
    </tr> 
    <tr> 
        <td><input type="text" name="s2" value="1" size="2" readonly="readonly"/></td> 
        <td><input type="text" name="name2_0"  size="20"/></td> 
        <td><input type="text" name="name2_1" size="20"/></td> 
        <td><input type="text" name="name2_2" size="20"/></td> 
        <td><input type="text" name="name2_3" size="15"/></td>    
        <td><input type="text" name="name2_4" size="15"/></td>    
    </tr>
    <tr> 
        <td><input type="text" name="s3" value="1" size="3" readonly="readonly"/></td> 
        <td><input type="text" name="name3_0"  size="20"/></td> 
        <td><input type="text" name="name3_1" size="20"/></td> 
        <td><input type="text" name="name3_2" size="20"/></td> 
        <td><input type="text" name="name3_3" size="15"/></td>    
        <td><input type="text" name="name3_4" size="15"/></td>    
    </tr>
    </table>
    <input type="submit" />
    </form>
    
    <script type="text/javascript">
    var form = document.getElementById("na");
    na.onsubmit = function() 
    {
        var needCheck = [];
    
        var flds = form.elements;
        for ( var f = 0; f < flds.length; ++f )
        {
            var fld = flds[f];
            var parts = fld.name.split("_");
            if ( parts.length === 2 && parts[0].substring(0,4) === "name" )
            { 
                if ( fld.value.replace(/\s/g,"") !== "" )
                {
                    needCheck[parts[0]] = true;
                }
            }
        }
        // all rows that need checking are in needCheck
        var oops = ""
        for ( var prefix in needCheck )
        {
            for ( var inpnum = 0; inpnum <= 4; ++inpnum )
            {
                var fldchk = form.elements[prefix + "_" + inpnum];
                if ( fldchk.value.replace(/\s/g,"") === "" )
                {
                    oops += "\n" + fldchk.name + " is blank"; 
                }
            }
        }
        if ( oops !== "" )
        {
            alert("Fix these errors:" + oops); // PLEASE don't use alerts...demo only!
            return false;
        }
        return true;
    }
    </script>
    </body>
    </html>
    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.


  •  

    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
    •