PDA

View Full Version : help with form validation script


Lifeforce
05-31-2003, 03:17 AM
I'm trying to design a script that works for all my forms so I dont have to write a new one for each form. This script works perfectly in IE but but not in Netscrap 7. Could anyone tell me why? I've narrowed it down to the textarea, for some reason the textarea gets by in NN7.

<html>
<head>
<script>
function validate(form)
{
x = form.elements.length
i = 0
while(i < x)
{
if((form.elements[i].value == "" )&&(form.elements[i].alt == "req"))
{
alert("please fill in all required fields")
return false
}
i++
}
}
</script>
</head>
<body>

<form method="post" action="test.php" onSubmit="return validate(this)">
<textarea alt="req"></textarea>
<input alt="req" type="text">
<input alt="req" type="text">
<input type="submit">
</form>

</body>
</html>

HairyTeeth
05-31-2003, 11:23 PM
There is no 'alt' atrribute for the textarea.

http://www.w3.org/TR/REC-html40/interact/forms.html#edef-TEXTAREA

IE must interpret alt incorrectly for that element. You will have to use the name or id attribute and add another condition in the if statement (is probably easiest).