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 2 of 2

Thread: Code

  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code

    Sir please tel me how to validate,when we add dynamic rows using javascript and for that rows we have to validate.I will send the code please do some help.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function validate()
    {
    var id=document.getElementById("ide");
    if(id.value.length==0)
    {
    //alert("invalid id");
    id.focus();
    return false;
    }
    var b=document.getElementById("uname");
    if(b.value.match(/^[a-zA-Z]{1,}$/)==null)
    {
    //alert("invalid name");
    b.focus();
    return false;
    }
    var c=document.getElementById("add");
    if((c.value.length<15)||(c.value.length>60))
    {
    //alert("invalid address");
    c.focus();
    return false;
    
    
    }
    
    
    var x=document.getElementsByName("email");
    for(var i=0;i<=x.length;i++)
    {
    
    if (x[i].value=="")
            {
            alert("Please fillup atleast one textbox");
            x[i].focus();
            return false;     
    }
            
    
    
    //var d=document.getElementsByTagName("email");
    
    
    var e=document.getElementById("phone");
    if(e.value.length==0)
    {
    
    alert("invlaid phone");
    e.focus();
    return false;
    
    
    }
    
    var f=document.getElementById("city");
    if(f.value.length==0)
    {
    
    alert("invlaid city");
    f.focus();
    return false;
    
    
    }
    return true;
    }
    
    
    </script>
    </head>
    
    <body onLoad="document.test.ide.focus();"/>
    <form name="test">
    
    <tr>
    ID:<input type="text" name="ide" id="ide" />
    NAME:<input type="text" name="uname" id="uname" value="" />
    <textarea name="add" rows="3" cols="10" id="add" ></textarea>
    
    
    </tr>
    </table>
    
    <br>
    <input type="submit" value="Submit" onClick="return validate();">
    </body>
    
    
    
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    var clone;
    function cloneRow(){
    var rows=document.getElementById('mytab').getElementsByTagName('tr');
    for(var count=0;count<=rows.length;count++)
    {
    onclick=validate();
    
    }
    var index=rows.length;
    clone=rows[index-1].cloneNode(true);
    var inputs=clone.getElementsByTagName('input'), inp, i=0 ;
    while(inp=inputs[i++]){
    inp.name=inp.name.replace(/\d/g,'')+(index+1);
    
    }
    }
    //onload=validate();
    function addRow(){
    var tbo=document.getElementById('mytab').getElementsByTagName('tbody')[0];
    
    tbo.appendChild(clone);
    
    cloneRow();
    
    
    
    
    //tbo.validate();
    }
    onload=cloneRow;
    
    
    </script>
    </head>
    <body>
    <form>
    <table id="mytab">
    <tr>
    <td>Email</td><td><input type="text" name="email" id="email" value=""></td>
    <td>Phone No.</td><td> <input type="text" name="phone" id="phone" value=""></td>
    <td>City</td><td> <input type="text" name="city" id="city" value=""></td>
    </tr>
    </table>
    <br>
    <input type="button" value="Add a new row" onclick="addRow()">
    </form>
    </body>
    </html>
    Last edited by VIPStephan; 09-30-2011 at 07:02 PM. Reason: wrapped code BB tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,245
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Form validation of the pattern if(f.value.length==0) is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    You should avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'go' or 'replace' or 'button' or 'radio' or 'parseInt'.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 09-30-2011 at 07:40 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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