Form Validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wayne

    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="Javas cript">
    function checkForm(form) {
    for (var i = 0; 1 < form.elements.l ength; 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='retur n
    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>
  • VK

    #2
    Re: Form Validation

    var OK = false;
    for (var i = 0; 1 < form.elements.l ength; 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 <wayne@lysoft.c om> wrote in message
    news:d92bd5e4.0 309021044.2ca9c 624@posting.goo gle.com...[color=blue]
    > 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="Javas cript">
    > function checkForm(form) {
    > for (var i = 0; 1 < form.elements.l ength; 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='retur n
    > 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>[/color]


    Comment

    Working...