A validation module for nodejs and express. It aims to give a convenient way to validate incoming POST data (which is the most common) and be as flexible as possible. This module is in development - feel free to contribute if you feel like it :)
npm install form-validate
In order to use the
req.Validator object and to access view helpers you have to register the form-validate middleware with express.
var express =validate = ;// ...Your express initialization logic...app;app;app;// ...var validationConfig =//You can configure certain aspects of the validation module;app;app;app;// ... Create http server
You can configure the behaviour of the
form-validate module by passing options when initializing the middleware:
var options =//You can configure certain aspects of the validation module;app;
You can configure the i18n module using this option.
||If this is true every value will be html escaped when using
||If this is true every value will be stripped from html tags when using
form-validation in an express context is pretty straight-forward:
Validators are the core functionality of node-form-validate. Input data can be validated to see if they meet certain
criteria. If they dont match, an error message will be generated. They can be added by using
This method of testing is not recommended. You should allow all Email addresses and validate it by sending an email to the given address.
Filters are applied before the input data is validated. Filters can parse the incoming data and prepare
it for validation or database input. They can be added by using
To get a list of all supported hash algorithms on your installation, see: http://nodejs.org/api/crypto.html#crypto_crypto_gethashes
Explicitly enable HTML escaping (enabled by default):
Disable HTML escaping:
Explicitly enable stripping of HTML tags (enabled by default):
Disable stripping of HTML tags:
To get validation errors simply call the asynchronous
Validator.getErrors(fn) method and provide a callback which gets called when all validations have been completed. The callback gets provided with an array containing all errors which resulted from the validation process.
Validator.getErrors()has to be called before the rendering took place.
The local variable
Validator is available in all express views.
/*** Returns the sanitized value of <code>fieldName</code> which ran through all* filters (if any)*/Validator/*** Returns all errors for this field as an array*/Validator/*** Returns true if this field has any error. You can use this to give the* field a certain class if an error occured*/Validator
form(action='/login', method='post')div(class=Validator.hasError('username') ? 'has-error' : '')label(for='username')=Usernameinput#username(type='text', name='username', value=Validator.getValue('username'))div(class=Validator.hasError('password') ? 'has-error' : '')label(for='password')=Passwordinput#password(type='text', name='password', value=Validator.getValue('password'))
Copyright (c) 2013 André Eckardt (http://github.com/korve, http://mindpress.de)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.