22

Trying to learn the basics of ASP.net MVC and cant quite understand why my client side validation isnt working.

I have this in both my web.config files (1 is global 1 is in the views folder)

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

I have this in my _layout.cshtmlfile

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/jquery.validate.min.js")

This is my view model for which im testing registration:

public class RegisterVM
    {
        [Required(ErrorMessage="Username Required")]
        public string username { get; set; }

        [RegularExpression(@"((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})")]
        [Required(ErrorMessage="Password Required")]
        public string password { get; set; }

        [Compare("password", ErrorMessage="Passwords do not match")]
        public string passwordConfirm { get; set; }
    }

And this is my html

@using(@Html.BeginForm()){

    @Html.LabelFor( model => model.username)
    @Html.EditorFor( model => model.username)
    @Html.ValidationMessageFor( model => model.username)<br />

    @Html.LabelFor( model => model.password)
    @Html.PasswordFor( model => model.password)
    @Html.ValidationMessageFor( model => model.password)<br />

    @Html.LabelFor( model => model.passwordConfirm)
    @Html.EditorFor( model => model.passwordConfirm)
    @Html.ValidationMessageFor( model => model.passwordConfirm)<br />

    <input type="submit" value="Register" />


}

Didn't want to post here, ive read a few topics on here related but cant seem to fix my issues, when i click submit it's making post/get requests.

Larry
  • 1,231
  • 4
  • 12
  • 18
  • 3
    I can't remember exactly, but is the order of files loaded in ` _layout.cshtmlfile` correct? didn't you missed a js from the first statement, and did you checked the files that has been loaded in browser? – Jahan Zinedine May 29 '13 at 10:47
  • View source shows all the files are loaded. – Larry May 29 '13 at 10:52
  • 3
    Did u try loading validate.min.js before validate.unobtrusive.min.js – Adil May 29 '13 at 10:52
  • 4
    Yes, the order is wrong. Unobtrusive validation depends on jQuery Validate. – Marcel N. May 29 '13 at 10:55
  • 2
    Fixed, my god spent so long wasting time on this wondering what on earth was wrong, wish I swallowed my pride an just asked!! – Larry May 29 '13 at 10:56

6 Answers6

26

Feels like this should work, I think you need to load validate.min.js before validate.unobtrusive.min.js

Adil
  • 3,248
  • 1
  • 22
  • 27
  • Already covered, that was the answer but comments above cant be marked as correct! Thanks – Larry May 29 '13 at 11:18
  • Yep I commented above but then realized that as answer it could be helpful for others – Adil May 29 '13 at 12:03
  • Hello, in my view file I set to layout=null and added the order of scripts @Scripts.Render("~/Scripts/jquery.validate.min.js")@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js") But the client script is not working, appreciate if you can tell me what else i need to modify? or suggestions. thank you – Benk Nov 18 '14 at 08:41
  • i also tried using jquery-1.7.1.min.js instead of 2.1.0 but still client side validation is not working, is there maybe a specific version I should be using? – Benk Nov 18 '14 at 08:49
  • In my case, I noticed some time scaffolding skips adding @Scripts.Render("~/bundles/jqueryval") at the end of layout file, I added it manually and it works. – sairfan Oct 31 '16 at 15:39
10

I had a similar problem and the only thing that fixed it was to manually call

 $.validator.unobtrusive.parse($('form'));

before I press the submit button.

I finally put it in document ready callback.

$(function () {
    $.validator.unobtrusive.parse($('form'));
});
Gudradain
  • 4,653
  • 2
  • 31
  • 40
  • After trying a lot of solutions without success, this one works very good. Thanks – IFink Feb 04 '16 at 11:20
  • I noticed this issue after bundling my scripts in BundleConfig.cs and my call to `$('#nameOfForm').validate();` wasn't working. Once i added this line it started working so thanks a lot – Chris Gong Aug 10 '17 at 10:07
3

In your page you have these two line in bottom of the page

please See carefully

@section Scripts {
 @Scripts.Render("~/bundles/jqueryval")
}
J.B.Vala
  • 301
  • 1
  • 3
  • 10
2

That's because you doesn't load jquery.validate.min.js before the unobtrusive one.

Amin Saqi
  • 18,549
  • 7
  • 50
  • 70
0
$('#divParent').load("url",
                     function () {
                     $.validator.unobtrusive.parse($('form'));}
                );

Note: This is required if you are loading 'form' or 'PartialView' dynamically inside 'div' using ajax calls

0

You need just reference these files on every page you need validation my friend!

<script src="~/Scripts/jquery.validate.js"></script>