Know that the id's and names came from database
You should note that you can still validate by the id. You just have to generate the javascript using php.
<script>
if(document.getElementById('<?php echo $row['elementid']; ?>').value.length <= 3) {
alert("<?php echo $row['displayname']; ?> must be more than 3 character long");
}
</script>
or
<script>
function checkField(obj, objname) {
if(obj.value.length <= 3) {
alert(objname + ' must be more than 3 character long');
}
}
</script>
<input id="<?php echo $row['elementid']; ?>" name="<?php echo $row['elementname']; ?>"type="text" onclick="checkField(this, '<?php echo $row[\'displayname\']; ?>')" /> <?php echo $row['displayname']; ?>
or
<script>
var elementidarray = new Array();
var i = 0;
</script>
<?php
$i = 0;
while($row = mysql_fetch_array($results)) {
?>
<script>
elementidarray[i] = '<?php echo $row['elementid']; ?>';
i = '<?php echo $i++; ?>';
</script>
<input id="<?php echo $row['elementid']; ?>" name="<?php echo $row['elementname']; ?>"type="text" /> <?php echo $row['displayname']; ?>
<?php
}
Oustside of doing it one of those ways you would have to look for all the input tags and other form tag
var allinputs = document.body.getElementsByTagName('input');
then loop through each to find the attributes
for (var i = 0; i < allinputs.length; i++) {
if (allinputs[i].getAttribute('type') == 'text') {
// check allinputs[i]
}