jQuery Form Validator plugin

Update

Latest update to v. 1.1 to enhance compatibility with the newest jQuery v. 1.3.1.

Overview

The jQuery Form Validator plugin allows to define simple, yet powerful, validators for web pages forms. It’s simple and configurable, and allows the definition of basic validations (required fields, numeric fields, integer fields, email fields) and also of regex over the fields in the forms.

Download

You can now download v. 1.1 of the jQuery Form Validator plugin.

Demo

On this web site you can find a working demo of the plugin.

Requirements

This plugin requires jQuery to be used in the web page. It has been tested with jQuery v. 1.2.6 and v. 1.3.1, but should correctly work with jQuery v. 1.2.x/1.3.1+.

Compatibility

The plugin has been tested on Windows XP, Windows Vista, Ubuntu (7.04, 7.10, 8.04 and 8.10) and Mac OS X 10.5 and is fully supported (at least) on the following browsers:

  • IE 8 beta, 7 & 6
  • Firefox 2.0+ and 3.0+
  • Opera 9 and 9.5+
  • Safari 3+
  • Google Chrome

Usage

The usage of the Form Validator is really simple. Example following:

var formValidator = new ValidatorClass();
formValidator.init({
form:           "#myFormID",
isRequired:     ['login', 'password'],
bindElement:    '#button',
callback:       function() {
alert('Ok!');
}
});

What it does

The method init() in the ValidatorClass instance enables the validator on the form with id=”myFormID“. Two text fields are required (those whose names are login and password).

The validator binds the object with id=”button” to the ‘click’ event: when it’s clicked, the validator runs the validation over the form. If the validation is successful, the callback function is called, otherwise an alert will popup telling which fields are wrong; those fields will also be coloured using a particular convention (red=required, green=numeric, blue=integer, yellow=email, lightgray=regex).

Complete example

The complete example for the script is:

var formValidator = new ValidatorClass();
formValidator.init({
form: '#example',
errorColors: {
isRequired: 'red',
isEmail:    'yellow',
isNumeric:  'green',
isInteger:  'blue',
hasRegex:   'lightgray'
},
isRequired:         ['name','email','number', 'date@select',
'street@select', 'description@textarea'],
isEmail:            ['email'],
isNumeric:          ['number'],
isInteger:          ['integer'],
hasRegex:           [
['field1', /d./, 'Error message'],
['field2@textarea', /s{5}(d{5}s{5}){1,10}$/, 'Another error']
],
allowNullNumbers:   true,
requiredFieldMsg:   'I campi in rosso sono obbligatori',
emailFieldMsg:      'I campi in giallo devono essere email',
numericFieldMsg:    'I campi in verde devono contenere numeri',
integerFieldMsg:    'I campi in blu devono contenere valori interi',
bindElement:        ['#button1', '#button2'],
bindEvent:          'click',
callback:           [callBack1, callBack2],
});

function callBack1() {
// Do something ...
}

function callBack2() {
// Do something different ...
}

Please note the definition of the required fields: for the five validation types it is possible to define an array of the field to control. Each item can be expressed in two forms:

  1. fieldName‘, which is equivalent to the CSS selector: input[name=fieldName]
  2. fieldName@domType‘, which is equivalent to the CSS selector: domType[name=fieldName] (es. domType = textarea)

Parameters

This is the explanation for every single parameter passed to the method. The default values are expressed in the square brackets next to the name:

  • form: the id of the form that will have the validation enabled
  • errorColor: a hash variable containing these colors for the wrong fields (colors applied to the fields after the validation):
    • isRequired[‘#FDD‘]: the CSS color that the field will have if it’s required and not filled
    • isEmail[‘#FFA‘]: the CSS color that the field will have if it has an incorrect email
    • isNumeric[‘#DFD‘]: the CSS color that the field will have if it’s not a number
    • isInteger[‘#CCE‘]: the CSS color that the field will have if it’s not an integer number
    • hasRegex[‘#CCC‘]: the CSS color that the field will have if it’s not compatible with the regex
  • isRequired: an array with the names of the fields that are required
  • isEmail: an array with the names of the fields that must be valid emails
  • isNumeric: an array with the names of the fields that must be numeric
  • isInteger: an array with the names of the fields that must have integer values
  • hasRegex: an array of arrays defining the regexes. Each inner array is in the form [‘fieldName’, /regex/, ‘Error message’]
  • allowNullNumbers[false]: if it is set to ‘true’ it allows numeric and integer field to have no values without being considered wrong
  • font-weight: bold;”>requiredFieldMsg[‘Red fields are required‘]: the alert message for required fields
  • emailFieldMsg[‘Yellow fields must contain valid emails‘]: the alert message for the email fields
  • numericFieldMsg[‘Green fields must contain numbers‘]: the alert message for the numeric fields
  • integerFieldMsg[‘Blue fields must contain integer values‘]: the alert message for the integer fields
  • bindElement: an array of the id’s of the objects to which the validator will be bound
  • bindEvent[‘click‘]: the event for which the validator will be bound to the previous elements
  • callback: an array of the functions to be called if the validation is successful. Each callback function is related to a single element: i.e. callBack1 is the callback function for ‘#button1’, etc.

API

  • init(): initializes the validator (read above for the parameters to be passed)
  • reset(): without any parameter, disables the validator

Terms of use

The jQuery Form Validator plugin is licensed under a Creative Commons License and is copyrighted (C)2008-2010 by Daniele Di Bernardo.

The license page can be found at: http://creativecommons.org/licenses/by-nc-sa/2.5/it/

794 thoughts on “jQuery Form Validator plugin

  1. Attractive section of content. I just stumbled upon your web site and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently quickly.

  2. Hello there! I could have sworn I’ve been to this blog before but after browsing through many of the articles I realized it’s new to me. Anyways, I’m certainly delighted I stumbled upon it and I’ll be book-marking it and checking back often.

  3. Hello there, There’s no doubt that your site could be having browser compatibility problems. When I look at your blog in Safari, it looks fine but when opening in IE, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Other than that, great site.

  4. An impressive share! I have just forwarded this onto a coworker who was conducting a little homework on this. And he actually ordered me lunch due to the fact that I stumbled upon it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending the time to discuss this issue here on your website.

  5. Today, with the fast life style that everyone leads, credit cards have a big demand throughout the market. Persons throughout every discipline are using credit card and people who aren’t using the credit cards have made up their minds to apply for just one. Thanks for giving your ideas on credit cards.

  6. I enjoy you because of your entire effort on this website. Ellie loves going through investigation and it’s obvious why. Almost all notice all relating to the lively means you convey valuable ideas by means of this website and therefore invigorate response from other people on this point and our own child has been starting to learn a lot. Take advantage of the remaining portion of the year. You’re the one conducting a pretty cool job.

  7. I must thank you for the efforts you’ve put in penning this site. I am hoping to see the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has encouraged me to get my own website now 😉

  8. Spot on with this write-up, I honestly believe that this website needs much more attention. I’ll probably be back again to read through more, thanks for the information.

  9. Youre so cool! I dont suppose Ive learn something like this before. So nice to find somebody with some original ideas on this subject. realy thank you for starting this up. this website is one thing that’s wanted on the web, someone with just a little originality. useful job for bringing something new to the web!

  10. Hello, you used to write fantastic, but the last several posts have been kinda boring… I miss your super writings. Past several posts are just a bit out of track! come on!

  11. Hi there, simply was alert to your weblog through Google, and located that it is truly informative. I’m going to watch out for brussels. I’ll be grateful when you proceed this in future. Lots of folks will likely be benefited from your writing. Cheers!

  12. After looking over a number of the blog posts on your web page, I truly like your technique of blogging. I bookmarked it to my bookmark site list and will be checking back in the near future. Take a look at my website too and let me know what you think.

  13. A fascinating discussion is worth comment. I do believe that you need to publish more about this subject matter, it might not be a taboo matter but typically people don’t discuss these topics. To the next! Many thanks.

  14. Hi there! I could have sworn I’ve been to your blog before but after browsing through many of the articles I realized it’s new to me. Regardless, I’m certainly pleased I came across it and I’ll be book-marking it and checking back often.

  15. My husband and i got now thankful that Jordan could finish up his preliminary research by way of the ideas he grabbed in your site. It’s not at all simplistic to just choose to be giving out secrets and techniques that people have been making money from. Therefore we do know we need the website owner to appreciate for this. All the explanations you have made, the simple site navigation, the relationships you will aid to promote – it’s got mostly powerful, and it’s facilitating our son in addition to our family consider that the idea is fun, and that is quite fundamental. Thanks for the whole lot!

  16. Hi, I do believe your blog may be having internet browser compatibility problems. When I look at your web site in Safari, it looks fine however when opening in IE, it’s got some overlapping issues. I merely wanted to give you a quick heads up! Other than that, wonderful site.

  17. After looking at a few of the blog posts on your web page, I truly like your technique of blogging. I saved it to my bookmark website list and will be checking back in the near future. Please check out my website too and tell me how you feel.

  18. The very next time I read a blog, I hope that it does not fail me as much as this particular one. I mean, I know it was my choice to read through, but I actually believed you would have something helpful to talk about. All I hear is a bunch of whining about something that you can fix if you were not too busy looking for attention.

  19. I figured out more a new challenge on this weight reduction issue. 1 issue is that good nutrition is extremely vital if dieting. A big reduction in bad foods, sugary foods, fried foods, sugary foods, beef, and bright flour products may be necessary. Retaining wastes parasitic organisms, and toxins may prevent goals for fat loss. While certain drugs quickly solve the condition, the horrible side effects will not be worth it, and they also never give more than a short lived solution. It can be a known idea that 95 of fad diet plans fail. Many thanks for sharing your opinions on this web site.

  20. Thank you, I’ve just been looking for info approximately this topic for a while and yours is the best I have discovered so far. But, what concerning the conclusion? Are you certain in regards to the source?

  21. I want to express my love for your generosity in support of men who require help on this important field. Your very own commitment to getting the message across appeared to be wonderfully valuable and has all the time permitted those much like me to arrive at their ambitions. Your personal informative tutorial signifies much to me and especially to my office workers. Warm regards; from everyone of us.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *