angular-validation 1.2.1

Client-side Validation is easy.
That's why Angular Validation is here!
Don't let Client-side Validation Crash your project.
Requirement
AngularJS > 1.2.0, 1.3.0-beta
DEMO
http://huei90.github.io/angular-validation/
http://plnkr.co/edit/rjIIkX (Bootstrap framework)
License
MIT
Install
Install with npm
npm install angular-validation
Using angular-validation
<script src="dist/angular-validation.js"></script>
<script src="dist/angular-validation-rule.js"></script>
angular.module('yourApp', ['validation']);
angular.module('yourApp', ['validation', 'validation.rule']);
Writing your Code
Add Valid Message (error, success) for validation required
required-error-message
and required-success-message
<label>Required</label>
<input type="text"
name="require"
ng-model="form.required"
validator="required"
required-error-message="Required"
required-success-message="Good Required"/>
Add Valid Message (error, success) for validation email
email-error-message
and email-success-message
<label>Email</label>
<input type="text"
name="email"
ng-model="form.email"
validator="email"
email-error-message="Error Email"
email-success-message="Good Email"/>
Use Default Valid Message
you don't need to give valid message
<label>Number</label>
<input type="text" name="number" ng-model="form.number" validator="number"/>
Don't show the Valid Message no-validation-message="true"
<label>Number</label>
<input type="text" name="number" ng-model="form.number" validator="number" no-validation-message="true"/>
<input type="text" name="number" ng-model="form.number" validator="number" no-validation-message="{{ noValidationMessage }}"/>
Add Valid Callback Function, invalid-callback
& valid-callback
<label>Required (Invalid Callback alert)</label>
<input type="text" name="requiredCallback" ng-model="form.requiredCallback" validator="required" invalid-callback='error("Must be Required");'/>
Select the validation method watch
blur
submit
submit-only
, default as watch
validationProvider.validate(form).success(callback).error(callback)
use callback to continue your submit
<label>Watch method</label>
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="watch"/>
<input type="text" name="number" ng-model="form.number" validator="number"/>
<label>Blur method</label>
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="blur"/>
<label>Submit method</label>
<form name="Form">
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="submit"/>
<button ng-click="form.submit(Form)"></button>
</form>
<label>Submit Only method</label>
<form name="Form">
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="submit-only"/>
<button ng-click="form.submit(Form)"></button>
</form>
<script>
$scope.form = {
submit: function (form) {
$validationProvider.validate(form)
.success(successCallback)
.error(errorCallback);
}
};
</script>
<form name="Form">
<input type="text" name="number" ng-model="form.number" validator="number" valid-method="submit-only"/>
<button validation-submit="Form" ng-click="form.submit()"></button>
</form>
<script>
$scope.form = {
submit: function () {
}
};
</script>
Setup a new Validation setExpression()
setDefaultMsg()
with RegExp
or Function
in config phase
<label>IP address (Custom setup the new validator - RegExp)</label>
<input type="text" name="ip" ng-model="form.ip" validator="ip"/>
<label>Huei (Custom setup the new validator - Function)</label>
<input type="text" name="huei" ng-model="form.huei" validator="huei"/>
angular.module('yourApp', ['validation'])
.config(['$validationProvider', function ($validationProvider) {
var expression = {
ip: /^(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]?)$/
};
var validMsg = {
ip: {
error: 'This isn\'t ip address',
success: 'It\'s ip'
}
};
validationProvider.setExpression(expression)
.setDefaultMsg(validMsg);
validationProvider
.setExpression({
huei: function (value, scope, element, attrs) {
return value === 'Huei Tan';
return $q.all([obj]).then(function () {
return true;
})
}
})
.setDefaultMsg({
huei: {
error: 'This should be Huei Tan',
success: 'Thanks!'
}
});
}]);
Check form whether valid, return true
if valid. checkValid()
Reset the specific Form. reset()
<form name="Form">
...
<button ng-disabled="form.checkValid()"></button>
<button ng-disabled="form.checkValid(Form)"></button>
<button ng-click="form.reset(Form)"></button>
<button validation-reset="Form"></button>
</form>
$scope.form.checkValid = validationProvider.checkValid;
$scope.form.reset = function (form) {
validationProvider.reset(form);
};
$scope.form.reset = function () {
};
Set the valid/invalid message style CSS
<span><p class="validation-valid">Your valid message here<p></span>
<span><p class="validation-invalid">Your invalid message here<p></span>
.validation-valid {
<!-- valid style -->
}
.validation-invalid {
<!-- invalid style -->
}
Custom the valid/invalid message style HTML in .config()
,
setErrorHTML(func)
setSuccessHTML(func)
, input should be a function
and given parameter
which is the valid/invalid message declared
in getDefaultMsg()
,and finally return the HTML code
.config(['$validationProvider', function ($validationProvider) {
$validationProvider.setErrorHTML(function (msg) {
});
$validationProvider.setSuccessHTML(function (msg) {
});
}]);
disable/enable show success/error message
default: true
Easily disable success/error message
.config(['$validationProvider', function ($validationProvider) {
$validationProvider.showSuccessMessage = false;
$validationProvider.showErrorMessage = false;
}]);
Built-in validation in angular-validation-rule
- Required
- Url
- Email
- Number
Anyone can give a PR
for this angular-validation for more built-in validation
To integrate this package with Bootstrap you should do the following.
Add the following LESS to your project
.ng-invalid.ng-dirty{
.has-error .form-control;
}
label.has-error.control-label {
.has-error .control-label;
}
Change the Error HTML to something like:
$validationProvider.setErrorHTML(function (msg) {
return "<label class=\"control-label has-error\">" + msg + "</label>";
});
You can add the bootstrap class .has-success
in a similar fashion.
CHANGELOG
See release
Q & A
###Can I validate the form when init ? #10###
<form name="Form">
<input type="text" name="number" ng-model="form.number" validator="number"/>
<button id="button1111" ng-click="form.submit(Form)">Show</button>
</form>
$timeout(function () {
$validationProvider.validate($scope.Form);
});
###What's the differentiate between validator-method submit
and submit-only
#4###
submit
: when user click submit, then start watching using watch
to validate
submit-only
: when user click submit
, doesn't validate through watch
until submit
button is clicked.
###When using checkValid() manually #19###
Before using checkValid()
, you have to execute submit
first to get the latest result.