Click to See Complete Forum and Search --> : Looping through form elements


Webskater
02-11-2003, 10:19 AM
Could anyone tell me the syntax please for looping through all the elements in a form, establishing if they are of type=text, establishing if they are empty and displaying an alert message that includes the name of the text box in the message. Thanks.

khalidali63
02-11-2003, 10:28 AM
Here you go.

cheers

Khalid


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-5"></meta>
<meta name="Author" content="Khalid Ali"></meta>
<title>Untitled</title>
<style type="text/css">

</style>
<script type="text/javascript">
function Process(){
var len = document.formName.length;
var frm = document.formName;
for(x=0;x<len;x++){
if(frm[x].type=="text" && frm[x].value==""){
alert("Text field ["+frm[x].name+"] must not be empty")
frm[x].focus();
return false;
}
}
}

</script>
</head>

<body>

<form name="formName" action="">
Name :<input type="text" name="name"></input><br>
Address:<input type="text" name="address"></input><br>
City :<input type="text" name="city"></input><br>
<input type="button" value="Process" onclick="Process()"></input>
</form>

</body>
</html>

Webskater
02-11-2003, 10:30 AM
Thanks very much Khalid.

khalidali63
02-11-2003, 10:51 AM
My pleasure as always..

:-)