Uncaught TypeError: Cannot read property 'settings' of undefined jquery validation

While I was working on jQuery validator I got into the following error:

Uncaught TypeError: Cannot read property 'settings' of undefined jquery validation

Reason of the error:

In jQuery validator they have a method called validate() that initiates all the settings for doing the validation. Now if we try to set some rules before the validate() method then it thorws the above error message. 

In my case I was trying to add a rule like below before executing the validate() methord: 

elementObj.rules('add', { customPhone: true});

The above rule can be only added after executing the validate method. 

Solution of the error: 

In my case the validate method was getting executed bit later. I took the advantages of setTimeout method. As anything inside setTimeout goes the end of the queue to execute. As soon as I put the following code the above error message was resolved:

setTimeout(function() {
   elementObj.rules('add', { customPhone: true});
}, 0);

Comments