Skip to content

Instantly share code, notes, and snippets.

@scizers
Last active August 29, 2015 14:02
Show Gist options
  • Save scizers/5f5ecf2004192a51762f to your computer and use it in GitHub Desktop.
Save scizers/5f5ecf2004192a51762f to your computer and use it in GitHub Desktop.
This is a angularjs directive for ensuring unique feilds in form
app.directive('ensureUnique', ['$http', function ($http) {
return {
require: 'ngModel',
link: function (scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function () {
if(scope[attrs.ngModel] !== undefined) {
$http({
method: 'POST',
url: '/' + attrs.ensureUnique,
data: {'field': scope[attrs.ngModel] }
}).success(function (data, status, headers, cfg) {
c.$setValidity('unique', data[0].isUnique);
}).error(function (data, status, headers, cfg) {
c.$setValidity('unique', false);
});
}
});
}
}
}]);
app.post('/checkMobile', auth.confirmUser, function (req, res) {
if (req.body.field !== undefined) {
var mobile = req.body.field;
var query = 'SELECT * FROM `users` WHERE `email` LIKE "' + email + '"';
utils.exec(query, {}, function (err, result) {
if (!result.length) {
res.json([
{isUnique: true}
]);
} else {
res.json([
{isUnique: false}
]);
}
})
} else {
res.json([
{isUnique: false}
]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment