SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: Form field validation
-
Mar 18, 2005, 11:33 #1
- Join Date
- Mar 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Form field validation
Hello,
I wrote a php page which contains some input fields of type text which are generated by using a for loop in php. The names for the input fields are generated by appending the value incremented in the for loop in php. eg. st0,st1,st2-----. Now i need to check using javascript that some value is entered in the input fields. How can i do that using a for loop in javascript.
The php code i used to generate the text fields are:
<form name=f method=post action=test.php>
<?php
$count=5;
for($i =0;$i<count;$i++)
{
echo "<input type=text name="st".$i>";
}
?>
</form>
The html o/p for the above code will be:
<form name=f method=post action=test.php>
<input type=text name=st0>
<input type=text name=st1>
<input type=text name=st2>
<input type=text name=st3>
<input type=text name=st4>
</form>
How can i check that whether a value is entered into the fields or not in javascript
-
Mar 18, 2005, 11:47 #2
- Join Date
- Nov 2004
- Location
- Portsmouth UK
- Posts
- 1,499
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
function Check(){
for (i=0;i<5;i++){
if (document.f['st'+i].value.length<1){
alert('st'+i+' is empty');
}
}
}
//-->
</script>
</head>
<body >
<form name=f method=post action=test.php>
<input type=text name=st0>
<input type=text name=st1>
<input type=text name=st2>
<input type=text name=st3>
<input type=text name=st4>
</form>
<input type="button" name="" value="Check" onclick="javascript:onload=Check();">
</body>
</html>
-
Mar 18, 2005, 12:18 #3
- Join Date
- Mar 2005
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hello,
Thanks for the post, that helped me solve it.
Bookmarks