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.
Your form ID needs to match the ID called to validate in the first line of the above jQuery example i.e. #frmEditUser.
//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!!