views:

44

answers:

1

Hi,

I've created a View which I do both server-side and client-side validation on the page using DataAnnotation attributes which decorate each input on the form. The validation take place when user tab out from a form control or when user clicks on "Submit" button which is of type "submit". <% Html.ValidationSummary("Please correct errors and try again."); Html.EnableClientValidation(); %> <% using (Html.BeginForm()) {%>

        <table width="100%">
            <tr>
                <td class="editor-label" width="20%">
                    <%= Html.LabelFor(model => model.From)%>
                    (Email)*
                </td>
                <td width="80%">
                    <%= Html.TextBoxFor(model => model.From, new { size=30, maxlength=100})%>
                    <%= Html.ValidationMessageFor(model => model.From)%>
                </td>
            </tr>
       <tr>
                <td class="editor-label" width="20%">
                </td>
                <td width="80%">
                    <input type="button" id="submitFeedback" value="Send" />
                    <input type="button" id="cancelFeedback" value="Cancel" onclick="closePopup();" />
                </td>
            </tr>

Since I'm posting form data through Async Ajax call using jQuery. It bypasses the validation set on the form. I want the validation to occur when when user clicks on the button that I use in place of "submit" button.

    $(function() {
        $('#submitFeedback').click(function() {
            $.ajax({
                type: "POST",
                url: "/Feedback/SubmitFeedback",
                data: { from: $('#From').val(), to: $('#To').val(), subject: $('#Subject').val(), body: $('#Comments').val() },
                success: function(feedback) {
                    alert(feedback);
                    closePopup();
                }
            });
        });
    });

I need to have access the Validation object of Microsoft Ajax library on the client side and I assume it has to have some "IsValid" method.

Thanks, Mohammad

A: 

Set the name of your javascript validation function in the Ajax.BeginForm() AjaxOptions:

Ajax.BeginForm("ActionMethod", new AjaxOptions(){OnBegin = "validateForm"}) { ...

This will call the validation function before the form is submitted.

Dave Swersky