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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.