I'm currently working on an angularJS project where I am trying to create a form for users to input their username. The application needs to validate if the username is available in the database and if it falls within a character length of 5 to 10.
<input type="text" name="uname" ng-model="user.uname"
ng-minlength="5" ng-maxlength="10" uniqueusername required/>
The uniqueusername directive is responsible for checking if the username is available:
app.directive('uniqueusername', function($http){
return{
require : 'ngModel',
restrict : 'A',
link : function(scope,elem,attrs,ctrl){
ctrl.$parsers.unshift(function(value) {
$http.get('/api/check'+value).success(function(data,status){
if(data.available=='true'){
ctrl.$setValidity('unique',true);
}
else{
ctrl.$setValidity('unique',false);
.........................................................................
Currently, I have noticed that when I input a username with less than 5 characters, it is still considered valid. This should not be the case. Interestingly, everything works perfectly fine when I remove the uniqueusername directive from the input field.
Thank you for your responses.