Tuesday 16 February 2021

MVC - Ajax - How to validate form and check if it is valid

 Watch this example on YouTube


 

1. Layout  - ensure you have     @Scripts.Render("~/bundles/jqueryval")

 

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap")  
    @Styles.Render("~/Content/themes/base/css", "~/Content/css")
    
    @RenderSection("scripts", required: false)
</body>
</html>

 

2. Index and Java

@model WebApplication1.Models.OrderCustomClass

@{ Html.EnableClientValidation();}
@using (Ajax.BeginForm("", "", null, new { @id = "frm" }))
{
    <div>
        @Html.TextBoxFor(x=>x.or.Column1)
        <input type="submit" onclick="DoSomething()" id="btnSubmit1" value="Submit" />    
    </div>
}
    @section Scripts{
        <script type="text/javascript">
            function DoSomething() {
                $("#frm").validate();
                var IsValid = $("#frm").valid();
                if (IsValid) {
                    alert('valid')
                }
                else {
                    alert('not valid')
                }
            }
        </script>
            }

No comments:

Post a Comment