Larry Larry - 1 year ago 99
C# Question

MVC 4 Client side validation not working

Trying to learn the basics of 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


This is my view model for which im testing registration:

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

[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


@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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download