Question

I am able to see the alert message here. Even after returning false, I can see my form posting it. Where is the code broken?

function ValidateForm(){
    var productName = document.addProduct.product_name;
    var partNumber = document.addProduct.part_number;
    var description = document.addProduct.description;
    var price = document.addProduct.price;

    var formelements = [productName, partNumber, description, price];
    formelements.forEach(function(obj) {
        if(obj.value=="") {
           obj.style.borderColor = "#FF0000"; 
           alert(obj);
           return false;
        }
    });

My HTML code

<form action="product_formhandler.php" name="addProduct" id="addProduct" onsubmit="return ValidateForm();" method="post">
    <table>
        <tr>
            <td>
                Product Name:
            </td>
            <td>
                <input type="text" id="product_name" name="product_name" />
            </td>
        </tr>
        <tr>
            <td>
                Part number:
            </td>
            <td>
                <input type="text" id="part_number" name="part_number" />
            </td>
        </tr>
        <tr>
            <td>
                Description:
            </td>
            <td>
                <textarea id="description" name="description" rows="8" col="25"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                Price:
            </td>
            <td>
                <input type="text" id="price" name="price" />
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">

                <input type="reset" id="reset" value="Reset" />
                <input type= "hidden" name="addproduct" value="1" />
                <input type="submit" id="addProductSubmit" name="action" value="Add" />
            </td>
        </tr>
    </table>
</form>
Was it helpful?

Solution

The function ValidateForm doesn't have a return statement. The only one you have is in the anonymous function you pass to forEach

  • Define a variable before you call forEach
  • Give it a default value (probably true)
  • Change it as you go around the loop (the specific logic will depend on what you are looking for, but will probably be if (condition) { retVal = false; } with no else)
  • Return it at the end

Such:

var retVal = true;
formelements.forEach(function(obj) {
    if(obj.value=="") {
    obj.style.borderColor="#FF0000"; 
    alert(obj);
    retVal = false;
});
return retVal;

Alternatively, use a traditional for loop:

for (var i = 0; i < formelements.length; i++) {
   var obj = formelements[i];
   if (obj.value == "") {
       obj.style.borderColor="#FF0000"; 
       alert(obj);
       return false;
   }
}

Although this approach will stop as soon as a single failure is found.

OTHER TIPS

You're returning false from your forEach loop, not your validation function. You'll need to do something like this instead:

/* Declare variable outside of loop which is set to true by default */
var forEachResult = true;
formelements.forEach(function(obj) {
    if(obj.value=="") {
    obj.style.borderColor="#FF0000"; 
    alert(obj);
    /* Rather than returning false, set the variable to false instead. */
    forEachResult = false;
    /* Break the loop. */
    return;
}
/* Return the variable which will either be true or false. */
return forEachResult;
function ValidateForm(){
    var productName=document.addProduct.product_name;
    var partNumber=document.addProduct.part_number;
    var description=document.addProduct.description;
    var price=document.addProduct.price;

    var formelements =[productName, partNumber, description, price];

    var isValid = true;
    formelements.forEach(function(obj) {
        if(obj.value=="") {
            obj.style.borderColor="#FF0000"; 
            alert(obj);
            isValid = false;
            return false;
        }
    });

    return isValid;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top