First off, to do this right you should ideally be using a module system on your front end. Something like CommonJS, RequireJS or Almond.js are all acceptable choices.
Then for validation, you should create a validation with interfaces/apis similar to something like underscore.string.js. It should basically validate the content of form inputs, which will usually be strings and numbers. These functions should know absolutely nothing about the DOM or client-side javascript. They should only know about the data you are interested in.
Building your validation library like this means it can be used in at least three different ways:
(1) use like the library being discussed in this thread
(2) use at the model layer on the client-side with a framework like backbone.js or angular.js for examples
(3) use on the server side if you are using something like node.js that allows javascript execution.
Now once you have that general purpose data validation library, you can achieve use (1) by coupling it with a tiny adapter library via the module system of your choice. In this adapter library you require(your_validation_library) as a local var and then create a chainable jQuery interface that takes the `this` variable which is usually bound to the element currently being processed when creating a jquery plugin and pipes the val() of that element to the appropriate function in your validation library.
One way to do this is to have a one to one mapping between some DOM attribute value (like a classname or a custom data attribute like data-validation) and the functions in your validation library. With this approach, you get that attribute value from `this` and use a hashmap to determine what validation function (or functions if your hash map contsains arrays of function names as the value in the k-v pairs) is appropriate.
Then for validation, you should create a validation with interfaces/apis similar to something like underscore.string.js. It should basically validate the content of form inputs, which will usually be strings and numbers. These functions should know absolutely nothing about the DOM or client-side javascript. They should only know about the data you are interested in.
Building your validation library like this means it can be used in at least three different ways: (1) use like the library being discussed in this thread (2) use at the model layer on the client-side with a framework like backbone.js or angular.js for examples (3) use on the server side if you are using something like node.js that allows javascript execution.
Now once you have that general purpose data validation library, you can achieve use (1) by coupling it with a tiny adapter library via the module system of your choice. In this adapter library you require(your_validation_library) as a local var and then create a chainable jQuery interface that takes the `this` variable which is usually bound to the element currently being processed when creating a jquery plugin and pipes the val() of that element to the appropriate function in your validation library.
One way to do this is to have a one to one mapping between some DOM attribute value (like a classname or a custom data attribute like data-validation) and the functions in your validation library. With this approach, you get that attribute value from `this` and use a hashmap to determine what validation function (or functions if your hash map contsains arrays of function names as the value in the k-v pairs) is appropriate.