PDA

View Full Version : Validate blank text field


NRastogi
09-09-2002, 11:43 PM
Hi guys,

In my form, I have input fields for entering qunatities (these fields generated dynamically depending upon the number of line items in the order). All the fields has same name assigned to it. So, there is no way to differentiate them.

Now my question comes, I want to display the message when user leave/make one or more fields "blank" and want to place the curson on the first blank field.

Please suggest me how could I do this.

Thanks in advance.

NRastogi

PS: There is one more input field on the page that displays "Date" and is under another form tag.

umm
09-10-2002, 05:42 AM
could you post the script please

Zvona
09-10-2002, 09:16 AM
<!-- Example Written by Zvona -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Check for blank textfields</title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1;" />
<script type="text/javascript">
<!--
function zIsFormFilled(oForm)
{
var aElem = oForm.elements;
var bHasBlankFields = false;
for (var iI=0;iI<aElem.length;iI++)
{
if (aElem[iI].type == "text" && aElem[iI].value.length == 0)
{
bHasBlankFields = true;
break;
}
}

if (bHasBlankFields)
{
alert("There are blank fields left");
aElem[iI].focus();
}

return (!bHasBlankFields);
}
// -->
</script>
</head>

<body>
<form action="" onsubmit="return zIsFormFilled(this);">
<table>
<tr>
<td><input type="text" /></td>
</tr>
<tr>
<td><input type="text" /></td>
</tr>
<tr>
<td><input type="text" /></td>
</tr>
<tr>
<td><input type="submit" value="Go" /></td>
</tr>
</table>
</form>
</body>
</html>