...

View Full Version : Code



pallaviss
09-30-2011, 01: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>

Philip M
09-30-2011, 07:36 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum