Skip to content

Instantly share code, notes, and snippets.

@vstymkovskyi
Forked from iladarsda/demo.html
Created April 18, 2019 20:05
Show Gist options
  • Save vstymkovskyi/5a95e738ee0431a83259dab27d6fdf71 to your computer and use it in GitHub Desktop.
Save vstymkovskyi/5a95e738ee0431a83259dab27d6fdf71 to your computer and use it in GitHub Desktop.
Create a custom $parser and add it to the ngModel controller, which in more human translation could mean for example, adding a custom validator to the ng-model of the input.
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<meta charset="UTF-8">
<title>$parsers demo</title>
</head>
<body>
<form name="sampleForm">
<input name="strongSecret" type="text" ng-model="strongSecret" strong-secret required>
<dir ng-messages="sampleForm.strongSecret.$error">
<div ng-message="numberValidator">Strong secret has to contain at least 1 number</div>
<div ng-message="uppercaseValidator">Strong secret has to contain at least 1 uppercase</div>
<div ng-message="sixCharactersValidator">Strong secret has to exactly 6 characters long</div>
</dir>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular-messages.js"></script>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
angular.module('app', ['ngMessages']);
angular.module('app').directive('strongSecret', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
function customValidator(ngModelValue) {
if (/[A-Z]/.test(ngModelValue)) {
ctrl.$setValidity('uppercaseValidator', true);
} else {
ctrl.$setValidity('uppercaseValidator', false);
}
if (/[0-9]/.test(ngModelValue)) {
ctrl.$setValidity('numberValidator', true);
} else {
ctrl.$setValidity('numberValidator', false);
}
if (ngModelValue.length === 6) {
ctrl.$setValidity('sixCharactersValidator', true);
} else {
ctrl.$setValidity('sixCharactersValidator', false);
}
return ngModelValue;
}
ctrl.$parsers.push(customValidator);
}
};
});
link: function(scope, element, attr, ctrl) {
// please note you can name your function & argument anything you like
function customValidator(ngModelValue) {
// check if contains uppercase
// if it does contain uppercase, set our custom `uppercaseValidator` to valid/true
// otherwise set it to non-valid/false
if (/[A-Z]/.test(ngModelValue)) {
ctrl.$setValidity('uppercaseValidator', true);
} else {
ctrl.$setValidity('uppercaseValidator', false);
}
// check if contains number
// if it does contain number, set our custom `numberValidator` to valid/true
// otherwise set it to non-valid/false
if (/[0-9]/.test(ngModelValue)) {
ctrl.$setValidity('numberValidator', true);
} else {
ctrl.$setValidity('numberValidator', false);
}
// check if the length of our input is exactly 6 characters
// if it is 6, set our custom `sixCharactersValidator` to valid/true
// othwise set it to non-valid/false
if (ngModelValue.length === 6) {
ctrl.$setValidity('sixCharactersValidator', true);
} else {
ctrl.$setValidity('sixCharactersValidator', false);
}
// we need to return our ngModelValue, to be displayed to the user(value of the input)
return ngModelValue;
}
// we need to add our customValidator function to an array of other(build-in or custom) functions
// I have not notice any performance issues, but it would be worth investigating how much
// effect does this have on the performance of the app
ctrl.$parsers.push(customValidator);
}
<ul class="error-msgs" ng-messages="sampleForm.strongSecret.$error">
<li ng-message="numberValidator">Strong secret has to contain at least 1 number</li>
<li ng-message="uppercaseValidator">Strong secret has to contain at least 1 uppercase</li>
<li ng-message="sixCharactersValidator">Strong secret has to be exactly 6 characters long</li>
</ul>
// remember, directive name must start with a lower case & use camel case naming convetion
angular.module('app').directive('strongSecret', function() {
return {
// limit usage to argument only
restrict: 'A',
// require NgModelController, i.e. require a controller of ngModel directive
require: 'ngModel',
// create linking function and pass in our NgModelController as a 4th argument
link: function(scope, element, attr, ctrl) {
}
};
});
<form name="sampleForm">
<input name="strongSecret" type="text" ng-model="strongSecret" strong-secret required placeholder="Please provide stron secret">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment