AngularJS form validation
http://sanji-io.github.io/sanji-validator
#Table of contents
- Requirements
- Browser Support
- Quick Configuration
- sanjiValidatorConfig provider
- sanjiValidator directive
- sanjiValidatorMethod directive
- sanjiValidatorSubmit directive
- beforeElementSelector attribute
- jQuery
- AngularJS
- lodash
- Chrome
- Firefox
bower install sanji-validator
This will copy the sanji-validator files into a bower_components
folder, along with its dependencies. Load the script files in your application:
<script src="bower_components/sanji-validator/dist/sanji-validator.js"></script>
angular.module('yourApp', ['sanji.validator']);
Array: Specifies the validators.
String: Default to be watch for global. Can be defined as watch, blur or submit.
angular.module('yourApp')
.run(function(sanjiValidatorConfig) {
sanjiValidatorConfig.setValidators({
range: {
rule: function(value, minlength, maxlength) { // this field shoud have minlength and maxlength HTML attribute
var length = value.length;
return (minlength <= length) && (length <= maxlength);
},
error: "The value should be in range ({{minlength}} ~ {{maxlength}})"
},
required: {
error: "This field is required."
},
ip: {
// rule can be defined as RegExp or function
rule: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
error: "This ip format is invalid."
}
});
sanjiValidatorConfig.setValidMethod('blur'); // set global field validate method
});
String: a comma separated string which represents the names of validators
<form ng-submit="submit()" name="form">
<label>
<span>IP: </span>
<input type="text" ng-model="row.ip" sanji-validator="ip,required">
</label>
<label>
<span>Desc: </span>
<input type="text" ng-model="row.desc" minlength="10" maxlength="30" sanji-validator="range,required">
</label>
<button type="submit" ng-disabled="form.$invalid">Save</button>
</form>
String: define the validate method of this form
put this if you want to validate before ngSubmit.
<form ng-submit="submit()" name="form" sanji-validator-method="blur" sanji-validator-submit>
<label>
<span>IP: </span>
<input type="text" ng-model="row.ip" sanji-validator="ip,required">
</label>
<button type="submit" ng-disabled="form.$invalid">Save</button>
</form>
If you want your message appears next to certain HTML tag, define before-element-selector attribute.
<div class="input-group">
<input name="idleTime"
type="text"
class="form-control"
ng-model="current.cellular.idleTime"
before-element-selector=".input-group"
sanji-validator="number,required">
<span class="input-group-addon">sec.</span>
</div>