MVC2 client side validation in JQuery modal dialog

Using MVC2 I currently have a view creating a jquery dialog containing partial edits. On submission, I'm looking for it for client side validation in an email class that has the required data annotation attribute for the email address. Server side validation works fine, but I want the user to fix the error in the modal dialog.

Below is the code

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm())
<div>
 <label for="EmailAddress">
                    Email Address :
                </label>
 <%= Html.TextBoxFor(model => model.Email.EmailAddress)%>
 <%= Html.ValidationMessageFor(model => model.Email.EmailAddress)%>
</div>

      

The scripts I download are

<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.3.2.min.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jqueryUI/js/jquery-ui-1.7.2.custom.min.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/Splitter/splitter-1.5.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/Scripts/Start.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/Scripts/extended/ExtendedControls.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery.validate.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/MicrosoftMvcJQueryValidation.js")%>"></script>

      

Looking at the generated html, I am not getting any JSON data generated for client side validation to work.

Any solutions are gladly appreciated. S

+2


a source to share


1 answer


I highly recommend using jquery script validation.

jquery.validate.js has all functions for client side validation in jquery dialog.

First of all add jquery.validate.js to your Site.Master:

<script src="/Scripts/Using/jquery.validate.js" type="text/javascript"></script>

      



and then write a script something like this:

    <script type="text/javascript">
    var createLinkObj;
    $(function () {

        $('#mydialog').dialog({
            autoOpen: false,
            width: 500,
            modal: true,
            buttons: {
                "OK": function () {

                    $("#myForm").validate({
                        rules: {
                            Name: {
                                required: true
                            },
                            Email: {
                                required: true,
                                email: true
                            }
                        },
                        messages: {
                            Name: " * ",
                            Email: {
                                required: " * ",
                                email: " Invalid e-mail."                        
                        }
                });

                $("#myForm").submit();
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });

    $(".mylink").click(function () {
        //change the title of the dialog
        createLinkObj = $(this);
        var dialogDiv = $('#mydialog');
        var viewUrl = createLinkObj.attr('href');
        $.get(viewUrl, function (data) {
            dialogDiv.html(data);
            dialogDiv.dialog('open');
        });
        return false;
    });
});
</script>

      

As you can see, when I click on mylink mydialog appears and before submitting myForm, I checked the myForm elements, Name and Email.

Think your form only contains name and email, and then you can validate those elms using jquery script validation.

0


a source







All Articles