A MooTools class for realtime, client-side validation of form inputs. Given a form it will fire events as inputs become valid/invalid as the user is typing. It will also, of course, alert you when the entire form is valid.
For now, specifying which inputs are validated and via what criteria are done through class names. This could probably switch over to data attributes at some point, but I don't want to do all that browser testing (help?).
Validators themselves are configurable, and a small set of them is provided to get you started (in the ValidateSimple.Validators object).
Note: this is not a drop-in "plugin" that will do everything for you. This code does not enforce and kind of UI whatsoever, it just fires events and lets you handle what is displayed. If you want a more powerful, hands-off validator use the standard MooTools More one. I created this to be more light-weight and have greater flexibility in when forms are validated.
In your HTML, add classnames of the form "validate-TYPE" where TYPE is something like "email".
<input name="email" type="email" title="A valid email, please" class="validate-email">
In your Moo code, do something like:
new ValidateSimple(form_element, { inputs: 'form input[type=text], form input[type=email]', onValid: function(){ // do something like activate the submit button }, onInvalid: function(){ // do something like deactivate the submit button }, onInputInvalid: function(input, errors){ // do something like show an error message somewhere, possibly based off the input's title attr // errors (2nd arg) will contain an array of strings - each string is the validation type that // failed. Example: ['email', 'no-troll'] } });
Here's what happens next:
What this means is that the first time a user fills out a given input, it will wait until they are finished to alert them. But, once they go back to an input alerting (of valid or invalid) will happen as they type. The entire form's validity is always based on each keyup.
Let's say you did this in your html
<input name="email" type="email" class="validate-email validate-no-troll">
You could create this validator anywhere in your Javascript:
ValidateSimple.Validators['no-troll'] = { test: function(input){ return !input.get('value').test(/<\s*script/i) && !input.get('value').test(/drop\s+table/i); } };
Now in your onInputInvalid callback, you can check for "no-troll" in the errors array, and do something to the troll you caught. ;7
A validator can optionally have a 'postMatch' method that will be called upon a successful test, if the test returned the result of a call to String.match. It will be passed the match data, and the input that was tested. This is useful for reformatting valid input a format of your choice, for normalization.
If a validator object definition includes async: true, that test will be treated as an asynchronous test. This is typically used for validations that require a call to your server. The test function is passed the input (as usual) and a method to pass your test result as a boolean. Note: asynchronous validators only run after all other validators on an input have passed (this is to save on network calls and general complexity). Here is an example:
ValidateSimple.Validators['password-dictionary'] = { async: true, wait: 100, test: function(input, handeResult){ new Request({ url: '/check_password', data: { password: input.get('value') }, onSuccess: function(resp){ handeResult(resp !== 'false', true); } }).send(); } };
You may also specify wait:N in your validator definition to wait Nms after the last successful (synchronous) validation attempt to call your asynchronous validator.
var vs = new ValidateSimple(form[, options]);
Activates the instance of ValidateSimple (attaches events and sill start firing events).
vs.activate();
Deactivates the instance of ValidateSimple (detaches events and sill start firing events).
vs.deactivate();
Validates all inputs and returns true for a valid form, false for an invalid form.
vs.validateAllInputs();
A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.
Found a bug in this plugin? Please report it this repository's Github Issues.
blog comments powered by Disqus