views:

331

answers:

1

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  A: 

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.

queen3
ok, let me make my part clear.. i have a validation summary where all the error messages are displayed. I have a server side call which validate the credit card, if doesn't exist it should append the error message to the existing validation summary. if it exists, i need to remove the displayed error message from the validation summary.
Nimesh
You didn't clarify the question, instead you added another requirements - now you want to remove error message, not only add one. You didn't say what is the server call - is it MS Ajax? jQuery? Now, my answer still applies - just add li element to the validation summary list. Or, you can have your own ul list next to the default one, with the same validation-summary-errors class - it will have single li element so you know what to add/remove. Notice that you have to know jQuery for your task - if you use AJAX for server call, of course.
queen3