HTML
<input type="text" name="username" ng-model="username"
ng-pattern="/^[a-zA-Z0-9]{4,10}$/" ng-unique="tableDB.userDBField" />
AngularJS
directive('ngUnique', ['$http', function (async) {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
elem.on('blur', function (evt) {
scope.$apply(function () {
var val = elem.val();
var req = { "username":val, "dbField":attrs.ngUnique }
var ajaxConfiguration = { method: 'POST', url: 'backendServices/checkUsername.php', data: req };
async(ajaxConfiguration)
.success(function(data, status, headers, config) {
ctrl.$setValidity('unique', data.status);
});
});
});
}
}
}
]);
Check the full document Form Validation: The AngularJS Way