By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Form Validation

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.