how to append some error messages on xval validation summary through javascript in MVC?

6.5k views Asked by At

how to append error messages on validation summary through javascript. i have added a validation summary, click on check button need to validate the credit card and retrieve the information back to the user, if the card is invalid i need to display that message in the validation summary. how can i do that? is there any way to programatically add the error messages to the validation summary using javascript?

1

There are 1 answers

2
queen3 On BEST ANSWER

First of all, you need to express your question better...

As far as I understand, you make AJAX call and if it fails you need to update the validation summary. Solution: use jQuery.

More details: Validation summary is rendered into the element with class "validation-summary-errors" - like this:

<ul class="validation-summary-errors">
   <li>First Name too long</li>
   <li>Invalid Email Address</li>
</ul>

So to update it you need to append a new li item to it. Here's how you do using jQuery:

$(".validation-summary-errors").append(
    "<li>" + "Your card is not valid" + "</li>");

Now, where to call the above code depends on how you check the credit card - jquery, ms ajax, etc.