Jump to content

Validation of several fields using one statement


jkkenzie

Recommended Posts

This will check all text inputs, but not things like radio buttons because those have default values.

<html>
<head>
<script language="JScript">
function validate()
{
var inputs = frm1.getElementsByTagName("input");
var errString = "";
for (i=0; i<inputs.length && inputs[i].type != "submit";i++)
{
	if (!inputs[i].value)
		errString += inputs[i].id+" must not be empty!\n";
}
if (errString)
	alert(errString);
}
</script>
</head>
<bodY>
<form id="frm1">
<input type="text" id="t1"><br>
<input type="text" id="t2"><br>
<input type="text" id="t3"><br>
<input type="text" id="t4"><br>
<input type="submit" onclick="validate()">
</form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.