Computer Science, asked by lilmag626, 1 year ago

Displaying multiple error messages in same alert in javascript validation

Answers

Answered by shree705
0
You've got a mistake here:

document.getElementById("error").innerHTML="<li>"+errorMsg[i]+"</li>"

You need to append errorMsg, not to assign it. For example like this:

document.getElementById("error").innerHTML+="<li>"+errorMsg[i]+"</li>"

If you want to display the error message on the top of the page, you should create div, where you want, give to this div the id and set innerHtml to this div:

<div id="AllErrors"></div> document.getElementById("AllErrors").innerHTML+="<li>"+errorMsg[i]+"</li>"

Also you could improve your validation and validate fields when a user is typing, not after he submitted all information.

And by the way, you add errors in tag <li>. So, you know that you should also add <ol> or <ul> to make a list?

share improve this answer

answeredDec 5 '12 at 6:47



colotiline
591●10●21

up vote1down vote

I rewrote your code a bit. Here is what I changed:

Instead of checking that the value equals " ", I have used the !operator.

I have used push on the array instead of appending by index. This means that there are no empty spots in your array. push adds an element to the end of the array, irrespective of the index.

I used a forEach loop on the array. This goes through all the enumerable elements, irrespective of how many there are.

So, the example:

JS

Answered by MoAmansheikh
0
How are things how can I do it
Similar questions