2
votes

I can't seem to get client-side validation to work in an asp.net mvc3 application. In the config I have

 <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />

In my _Layout.cshtml I have

 <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

In my page I have

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

I am just playing around using mvc 3 with ef 4 and my model class just has a field with that is required but client-side validation is not triggering. It's always posting back. Any ideas?

3
Can you post the model that you're expecting validation for? - Jesse
The validation does happen. It just happens on the server-side. It's not occurring on the client-side. - daustin

3 Answers

0
votes

What are you using in your Model for validation? Here is a quick and easy way:

using System.ComponentModel.DataAnnotations;

namespace YourNs
{
    public class YourModel
    {
        [Required]
        public string SomeRequiredString { get; set; }
    }
}

Now in your view for this model, if you have the necessary scripts referenced there should be client side validation. It's all through the [Required] attribute.

0
votes

I dont know is it feasible for your situation, but I get the validation by calling the .validate() function:

  var validator = $('#formData').validate();
       if ($('#formData').valid()) {
          //alert('valid');
          return true;
  }

Hope this could help

0
votes

Make sure these appSettings are in your root Web.config:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>