Form Validation

W

Wayne

The script I have written below works perfect to make sure that each
text field in a form is not left with the default value of "0". The
problem is that I need it to only produce the alert if all fields are
left with the value or "0". In other words if one of the fields has
been changed to a "1" but the other are still all "0" then it should
not produce an alert. I am new at javascript and it took me over an 1
1/2 to get this far and I am stuck. Any help would be greatly
appreciated.


<SCRIPT LANGUAGE="Javascript">
function checkForm(form) {
for (var i = 0; 1 < form.elements.length; i++) {
if (form.elements.value == 0) {
alert("Fill out All fields.")
return false
}
}
return true
}
</script>

<form method="post" action="http://www.balls.com" onSubmit='return
checkForm(this);'>
Item 1 : <input type="text" name="quantity1" value="0"><br>
Item 2 : <input type="text" name="quantity2" value="0"><br>
Item 3 : <input type="text" name="quantity3" value="0"><br>
<input type="submit" value="send">
</form>
 
V

VK

var OK = false;
for (var i = 0; 1 < form.elements.length; i++) {
if (form.elements.value) {
OK = true; break; // at least one field is not equal 0 - break the loop
}
}
if (OK) {...}
else {alert(...)}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top