directive angularjs to check mail used or not -
i want check if email used or not (communication back-end) error message not shown in screen.
the verficiation function service :
demoapp.factory('verifyemail', function($http) { return function(mail) { var test=mail; var urll="http://localhost:8080/app/personne/verifmail?msg="; var aplrest=urll+test; var ch3=aplrest; return $http.post(ch3) };});
the directive (with of @jsonmurphy)
demoapp.directive('existto', ["$q","verifyemail",function ($q, verifyemail) { return { require: "ngmodel", scope: { othermodelvalue: "=existto" }, link: function(scope, element, attributes, ngmodel) { // note change $asyncvalidators here <------------- ngmodel.$asyncvalidators.existto = function(modelvalue) { var deferred = $q.defer(); verifyemail(modelvalue).then(function(respons) { // respons can {exist="true"} or{exist="false"} var rep=respons.exist; deferred.resolve(rep); }); return deferred.promise; }; scope.$watch("othermodelvalue", function() { ngmodel.$validate(); }); } }; }]);
the html file:
<label>email</label> <input type="email" name="email" class="form-group" ng-model="registration.user.email" ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,8})$/" required exist-to="registration.user.email"/> <span style="color:red" ng-show="registrationform.email.$dirty && registrationform.email.$invalid"> <span ng-show="registrationform.email.$error.required">champs email obligatoire.</span> <span ng-show="registrationform.email.$error.pattern">invalid email address.</span> <span ng-show="registrationform.email.$error.existto">mail exist.</span> </span>
this solution
demoapp.directive('existto', ["$q","verifyemail",function ($q, verifyemail) { return { require: "ngmodel", scope: { othermodelvalue: "=existto" }, link: function(scope, element, attributes, ngmodel) { // note change $asyncvalidators here <------------- ngmodel.$asyncvalidators.existto = function(modelvalue) { var deferred = $q.defer(); // window.alert("l email tester "+modelvalue); verifyemail(modelvalue).success(function(respons) { // respons can {exist="true"} or{exist="false"} // window.alert("la reponse " +respons); var rep=respons.exist; // window.alert("la reponse " +rep); if (rep =="false") { // username available deferred.resolve(); } else { deferred.reject(); } }); return deferred.promise; }; scope.$watch("othermodelvalue", function() { ngmodel.$validate(); }); } }; }]);
Comments
Post a Comment