Search code examples
jqueryasp.netasp.net-mvcclient-side-validation

Validating form with Model State on Client Side in MVC


I am trying to validate the form on button click. As Form (view) is bounded with Model and model properties are wrapped with Data-Annotations, so before any other action, I want to validate the form with jQuery.

$("#btnSubNext,#btnSubLast").on('click', function () {
        $("#myform").validate();
        if ($('#myform').valid()) {

        }
    });

But it is not working properly.

Note: myform is id of form as mentioned below:

@using (Html.BeginForm("CCEAddUpdateScheduleA", "CCESchedule", FormMethod.Post, new { 
@id = "myForm", @name = "myForm", @class = "myForm" }))
    {.....
}

Solution

  • Do you have ClientValidationEnabled and UnobtrusiveJavaScriptEnabled as true in Web.config? You also need jquery.validate and jquery.validate.unobstrusive. Check http://www.asp.net/mvc/overview/older-versions/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript