python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions