Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Client Validation with jQuery using Html.ValidationMessage?

I am trying to tap into the HTML.ValidationMessage() element to write so really custom client side validation with jQuery. Is this even possible? My server side validation is displayed using the HTML.ValidationMessage(), and I am wondering how I would access that element to display a custom message using jQuery before a form submit.

I know there are a few plugins out there to do some of this stuff, but I would really like complete control with my validation.

Here is a bit of the Javascript code I have currently. Basically it adds the validation "class" to the input element on submit, however not sure how to access the Html.ValidationMessage to output something like "Email required."

    <script type="text/javascript">
    $(document).ready(function() {
        $("input[type=submit]").click(function() {

            var valid = true;

            // email blank
            if ($("input[name='email']").val().length < 1) {
                $("input[name='email']").addClass("input-validation-error");
                valid = false;
            }

            return valid;
        })
    });

</script>

And Code from the View:

       <p>
            <label for="email">
                Email:</label>
            <%= Html.TextBox("email") %>
            <%= Html.ValidationMessage("email") %>
        </p>
like image 712
aherrick Avatar asked Mar 19 '09 11:03

aherrick


People also ask

How can use client-side validation in jQuery MVC?

config" file. The above properties are set True by default which means MVC 5 platform ensures that client side validation on form validation is on. For jQuery form validation to work, we set "HtmlHelper. UnobtrusiveJavaScriptEnabled = false;" property false in the register form instead of "web.

How can use client-side validation in ASP NET MVC?

Enable Client-Side Validation in MVC For enabling client side validation, we required to include the jQuery min, validate & unobtrusive scripts in our view or layout page in the following order. The order of included files as shown above, is fixed since below javascript library depends on top javascript library.

How we can use jQuery validation plugins in MVC?

The jQuery validation plugin leverages a CSS selector like syntax to apply a set of validation rules. You can download the plugin (js) file from jQuery website. The password and confirm password objects are matched by the validation plugin for you and shows the message on the equalTo attribute if they don't match.


2 Answers

You could write a ton of code to do this. Or you could just use xVal, which is free and does exactly this, plus much more. Put value annotations on your .NET types, add a couple of simple things to your aspx, and you get jQuery validation for free. Plus it comes with providers for popular frameworks, and it's very customizable in case you need more complex validation.

I see no need to reinvent the wheel here.

like image 129
Craig Stuntz Avatar answered Sep 28 '22 05:09

Craig Stuntz


Would it be an idea to just use the jquery form validation plugin for client side ? It's easy to implement in the view code. You add a few tags to the input boxes and add a few lines of javascript in the header and you're done. Ofcourse you'd still have to code anything that's 'very custom' so to speak.

like image 35
Morph Avatar answered Sep 28 '22 03:09

Morph