469,327 Members | 1,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Form Validation

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[i].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>
Jul 20 '05 #1
1 3038
VK
var OK = false;
for (var i = 0; 1 < form.elements.length; i++) {
if (form.elements[i].value) {
OK = true; break; // at least one field is not equal 0 - break the loop
}
}
if (OK) {...}
else {alert(...)}

Wayne <wa***@lysoft.com> wrote in message
news:d9**************************@posting.google.c om...
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[i].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>

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Phil Powell | last post: by
4 posts views Thread by TG | last post: by
16 posts views Thread by Hosh | last post: by
9 posts views Thread by julie.siebel | last post: by
27 posts views Thread by Chris | last post: by
11 posts views Thread by Rik | last post: by
5 posts views Thread by lucyh3h | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.