Hi Friends, Right now I am working on CakePHP project in which I have to use jQuery Validation Library for validation of forms. jQuery validation library is really best option to make validation for any form because it is having too many options and we can use it very easy. But in this validation by default we have to use element name for any validation instead of class name or id of element, for example :
$().ready(function() { // validate signup form on keyup and submit $("#signupForm").validate({ rules: { firstname: "required", }, messages: { firstname: "Please enter your firstname", } }); });
You can see in above code “signupForm” is form id, But you can also see that to make First Name required we use “firstname”(which is the name of text box element of First Name). If I was using custom PHP language then I was not having any issues if this validation use name of element for validation instead of id or class, But in cakePHP when we create any element then it created it name like this :
data['ModelName']['firstname']
So we can’t use this name in this validation, otherwise it will not work. So I stuck into one big problem that I want to use this library but can’t use it with name and I can’t change name of cakePHP otherwise it will increase my code time while working on other functionality related to this form.
So I googling on internet to find solution for it because i knew that I am not only one developer who is using cakephp and want to use this library and having same issue like me. I got some clues but didn’t find perfect solution for it. But based upon on that solution I tried many things and finally I made jQuery validation library working with id of element.
So let me explain how we can do it.Lets take one example.We have one form which is taking information “First Name” and “Last Name” and we want to validate it with jQuery validation library. So form id is “testingform” and id of First name is “first_name” and Last Name id is “last_name”.So js code to validate this field would be like this :
$().ready(function() { // validate the form when it is submitted $("#testingform").validate(); $("#first_name").rules("add", { required:true, messages: { required: "Please Enter Title of Project." } }); $("#last_name").rules("add", { required:true, messages: { required: "Please Enter Description of Project." } }); /* You Can add oter fields like above Here with messages. But remember you have to mention $("#testingform").validate(); first and then write all other code */ });
You Can add other more fields like above,but remember you have to mention $(“#testingform”).validate(); first and then write all other code for more fields to validate.
So this is way to use id of input box or element while validation form by jQuery Validation Library. You can post you problems, thoughts in comments.
I am PHP Freelancer, Freelance PHP Developer having more than 5 years of experience, So if you want to do any projects then you can contact me.
Hostgator Hosting in 1 Cent Coupon Code – “1CENTHECOUPON” (Without Quotes).