ajax jquery example post

5 Best Ways To jQuery Validation in MVC 5 and Examples

How to validate a form in jQuery?

The basic steps as,

/* Fire Required Valaidate */
$(function(){
   $("#frmEditUser").validate({
      rules: {
         name: {
            required: true
               }
         },
         messages: {
            name: "Required Field."
         }
     });
});

Here is what the validation code might look like if you have a form with your input fields FirstName and LastName as I motions in below example and you makes sure that the names of your input fields match your jQuery below example.

Your form ID needs to match the ID called to validate in the first line of the above jQuery example i.e. #frmEditUser.

This is for JQuery.Validate adding dynamic rules with messages with example

//HTML CODE
<form id="frmEditUser" class="color-text">
    <input type="text" name="FirstName" />
    <br/>
    <input type="text" name="LastName" />
    <br/>
    <input type="submit" />
</form>

<!-- Load jQuery and jQuery-Validate scripts -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="js/jquery-validate.js"></script>

//jQuery CODE
$(document).ready(function () {
   //#region THIS METHOD IS USED TO VALIDATE USER DETAIL AND SERVICES.
    $('#frmEditUser').validate({ // initialize the plugin
        rules: {
            FirstName: {
                required: true
            },
            LastName: {
                required: true
            }
        },
        messages: {
            FirstName: {
                required: "Enter First Name."
            },
            LastName: {
                required: "Enter Last Name."
            }
        }
    });
    //#endregion
});

//CSS CODE
.color-text{
  color:red;
}

THE OUTPUT LIVE http://jsfiddle.net/ep2d6edx/

I hope you are enjoying with this post! Please share with you friends. Thank you!!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.