The excellent Jörn Zaefferer has written a much better, feature complete jQuery input validation plugin which does the same job as mine originally set out to do.
As far as jQuery plugins go, I'll be the first to admit this is perhaps not like the others.
Unlike most other plugins, it is fair to say ValidText may require a little tweaking to get it doing exactly what you want it to do. That being said, the ability to define your own form validation rules - and the responses to those rules - makes ValidText powerful. If you have any 'non-standard' requirements (maybe you want to validate a string against a strict format, such as an SKU or a date), this could well be the form validation plugin for you.
<input type="text"> and <textarea> elementsValidText uses a concept of "rules" and "actions". The plugin itself is tiny, it is the ability for you to apply your own rules and/or actions to it that is key.
Rules define the allowed format(s) a text input element can accept. A good example of a rule is one which checks if an email address is valid: if the email address entered by the user is OK, the rule passes, but enter an invalid email address and the rule fails. Rules must only return true or false. There are a few example rules listed below which you are welcome to use in your own project or modify as you see fit (though a credit is always welcome).
Actions define what should happen when a rule fails (e.g. show an error message) and what should happen when that rule passes (e.g. hide that error message). Again, there are example actions listed below.
Rules and actions are regular JavaScript objects. ValidText provides convenient placeholders for you to keep your rules and actions called, you guessed it, ValidText.rule and ValidText.action. Define a rule as follows: ValidText.rule.MyNewRule = { ..... } placing your declaration after the plugin itself. Rules and Actions, once declared, can be reused in any combination you see fit.
Coming soon.
Rule objects contain three variables: allowpaste, maxlength and dictionary and one function: validate.
ValidText.rule.MyRule = {
allowpaste: [boolean], // Can the user paste from the clipboard into the text field?
maxlength: [integer], // Max number of characters a user can enter. Use 0 for unlimited.
dictionary: [string], // String of allowed characters the user can type. Use an empty string to allow all characters
validate: function(oInput, bFinalState) {
[your javascript code] // Code to be executed while the user is typing
if (bFinalState == true) {
[your javascript code] // Code to be executed when the user leaves this text field
}
}
}Action objects contain two functions: invalid and valid.
ValidText.action.MyAction = {
invalid: function(oInput) {
[your javascript code] // Code to be executed when a rule fails
},
valid: function(oInput) {
[your javascript code] // Code to be executed when a rule passes
}
}ValidText accepts a rule and an action as it's two inputs. It is up to you to use the rules and actions listed below or define your own to make your form input work as you wish.
$("#currency").ValidText(ValidText.rule.Currency,
ValidText.action.CurrencyError);
$("#emailaddress").ValidText(ValidText.rule.Email,
ValidText.action.EmailError);Coming soon.
Coming soon. Do you have a rule or action you've written you'd like to be listed here? Please let me see them.
<form> submission.You are welcome to download and use this plugin which is distributed under the MIT licence. If you're interested to see other jQuery work I've done, checkout JAWStats web analytics.
Useful stuff:
Code Snippets:
Recommended reading: