pallaviss
09-30-2011, 12:53 PM
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.
<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>
<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>