Last active
August 29, 2015 14:07
-
-
Save dkumar431/55dfb191710bb60c7bc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sampleApp.directive('myUsername', [ function(){ | |
// Runs during compile | |
return { | |
require: 'ngModel', | |
restrict: 'A', | |
link: function($scope, iElm, iAttrs, controller) { | |
controller.$parsers.unshift(function(value){ | |
//allow only alphanumeric characters | |
var reg = /^\w+$/ ; | |
var isValidUsername = reg.test(value); | |
controller.$setValidity('username',isValidUsername); | |
return isValidUsername? value:undefined; | |
}); | |
controller.$formatters.unshift(function(value){ | |
var reg = /^\w+$/ ; | |
var isValidUsername = reg.test(value); | |
controller.$setValidity('username',isValidUsername); | |
$scope.registrationForm.username.$setViewValue($scope.registrationForm.username.$viewValue); | |
return value; | |
}) | |
} | |
}; | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" name="username" ng-model="userData.username" ng-model-options="{updateOn: 'blur'}" my-username> | |
<span ng-show="registrationForm.username.$dirty && registrationForm.username.$error.username"> | |
Invalid Username. | |
</span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment