Question: I'm using javascipt to validate a form and the code is like below,

```
function validateForm() {
        console.log("start check form");
        var errors = [];
        var form = document.getElementsByTagName('form')[0];

        /* 
        * Store appropriate form elements.
        * There are many ways to do this, but I'm keeping it 
        * simple for the example
        */
        var fnElem = document.getElementById("name");
        var lnElem = document.getElementById("phone");
        var firstName = fnElem.value;
        var lastName = lnElem.value;
        // etc...

        // validate required fields
        if (firstName === "") {
            errors.push({
            elem: firstName,
            message: "name can't be blank"
            });
        }

        if (lastName === "") {
            errors.push({
            elem: lastName,
            message: "phone can't be blank"
            });
        }

        for(var i=0; i<errors.length;i++){
            alert(errors[i].message);
        }
        return false;   
    }

```

and in the for loop it will alert lots of times in the windows.
how can I combine all the alert into one single message..and only alert once.

I know that I can define a string and append one by one, But that seems so fool.
Is there a better way to do this.

Answer: 
Change your loop to append the message to a variable, then alert after the loop:

```
var messages = "";
for(var i=0; i<errors.length;i++){
    messages += errors[i].message + "\n";
}
alert(messages)

```
```
for( var sAlert = '', i = 0, iL = errors.length; i < iL; ++i ) {
    sAlert += errors[i].message + '\n';
}
alert(sAlert );

```

I didn't had a closer look at your code but if everything is fine this should solve it :)