views:

35

answers:

2

Hello All, i want to know that can we create two instance of same validator method in one js??

now let me tell u what i want exact

i have two pages basic.php another is contact.php having forms, name is basicForm and contactForm respectively , now i want to include one extrenal JS (validForm.js) file where i want to put both forms validator definations, but unable to succeed.

like

validForm.js

var validator = jQuery("#basicForm").validate({
            some rules...           
        }); 

var validator = jQuery("#contactForm").validate({
           other  rules...          
        });

so i want to know

  • may i invoke two instance of var validator in same js? or
  • do i have to use different js( having validator method) for different pages?
A: 

Name those validators according to the form they're supposed to validate

var basicFormValidator = jQuery("#basicForm").validate({
            some rules...           
        }); 

var contactFormValidator = jQuery("#contactForm").validate({
           other  rules...          
        });
Mchl
@Mchi, i tried your sugested way before, but variable name strictly shoud be `validator` when we use validator plugin (AFAIK)
JustLearn
Ive no experience with jQuery, so it might be the case, although it would seem a bit strange...
Mchl
A: 

I suggest you to do not create separate js file for each form validation (like your: validForm.js), as number of external js call will decrease performance of your page in terms of speed.

So if possible make all validation separate for each page. Write the script on each page.

If you have lots of custom validation, then you can add them to jquery.validate.js file.

Vikas
how to add custom validation in `jquery.validate.js`
JustLearn
Well I didn't do it, But Once had the issue that, I had ambiguity on `required` method. My CSS was also having the class with the name: `required`. So whenever I add a `required` validation through the class property, it also apply the CSS! At that time I started messing with **Other's** code. I renamed the method `required` to `required1` in jQuery.validate.js file. Then I realized that all the validations are around the **value** and **element**. Just see one of the function: `maxlength: function(value, element, param)`. Now I can say Happy learning!
Vikas