views:

24

answers:

2

Hey all

I am doing validation server side, If fails I am passing back json data with the errors.

I'm not worried about displaying the errors on page but happy with just a simple alert().

How can I display the errors nicely while using a $each function with jquery.

Here is some json data.

{"error":["The Firstname field is required.","The Lastname field is required.","The Address 1 field is required.","The City field is required.","The Zipcode field is required.","The Receive Mailing List field is required."]}

Displaying messy would be like

$.each(msg.error, function(k, v) {
 alert(v);   
});

Hope you can advise.

+3  A: 

If you want an alert but not individually for each message, you can just use .join() on the array here, like this:

alert(msg.error.join('\n'));

You can give it a try here, you'll get one alert with a message on each line, like this:

The Firstname field is required.
The Lastname field is required.
The Address 1 field is required.
The City field is required.
The Zipcode field is required.
The Receive Mailing List field is required.

Nick Craver
A: 

Please don't do that. Nothing is more annoying than a bunch of alert messages. If you want to display all the errors then combine all the errors into one string and display them with one alert.

Galen
I don't think the op wants an alert message for each error. All the errors listed in one alert box (as Nick has done above) would not be that bad (to a limit). The validation summary control allows you to do this as well (I mention this as an example, not a suggestion).
mpminnich
"happy with just a simple alert()."
Galen
@Galen - Since you're quoting exactly, "a" isn't plural :)
Nick Craver
exactly why i recommended only one alert. read my answer!
Galen