Skip to content

Instantly share code, notes, and snippets.

@ssskip
Last active March 17, 2016 09:25
Show Gist options
  • Save ssskip/4ccbce396364b92da1e2 to your computer and use it in GitHub Desktop.
Save ssskip/4ccbce396364b92da1e2 to your computer and use it in GitHub Desktop.
form validation
angular.module("kp.account")
/**
* @ngdoc directive
* @name kp.account.directive:kpNicknameValidation
* @description
* Defines validNickname method.
* length between 5-20,less than 5 digit numbers
*/
.directive("kpNicknameValidation", function() {
return {
require: 'ngModel',
link: function(scope, ele, attr, ctrl) {
ctrl.$validators.validNickname = function(modelValue, viewValue) {
//required
if (!viewValue) {
return false;
}
var length = viewValue.length;
//length between 5-20
if (length > 20 || length < 5) {
return false;
}
//less than 5 digit numbers
var matched = viewValue.match(/[0-9]/g);
if (matched !== null && matched.length > 5) {
return false;
}
return true;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment