Do not allow e-mail registration and or user in Angularjs duplicity

Asked

Viewed 304 times

0

Please, I can’t finish this simple code...

Should return the error "User already registered" or "Email already registered", please help me fix the Function below, I’m a little layman, more I’m learning. I would like to present the error, and if possible perform a single Function, to avoid mentioning doubly.

Please help me out.

angular.module("app", []);

angular.module("app").controller("menuCtrl", function ($scope) {
   
	
    $scope.listagem = [
        {nome_completo: "João", email: "[email protected]", usuario: "joaonovo"},
        {nome_completo: "Maria", email: "[email protected]", usuario: "mariavelha"},
        {nome_completo: "Carlos", email: "[email protected]", usuario: "carlosdoido"}   
    ];

   
    $scope.verificarDuplicidadeEmail = function(domScope, object){
        for(var i = 0; i < $scope.listagem.length; i++) {
            if($scope.listagem[i].email === object.email 
               && $scope.listagem[i] !== object) {
                domScope.listaForm.email.$setValidity('duplicado',false);
            }
            else {
                domScope.listaForm.email.$setValidity('duplicado',true);
            }
        }
    };
  
  
    $scope.verificarDuplicidadeUsuario = function(domScope, object){
        for(var i = 0; i < $scope.listagem.length; i++) {
            if($scope.listagem[i].usuario === object.usuario 
               && $scope.listagem[i] !== object) {
                domScope.listaForm.usuario.$setValidity('duplicado',false);
            }
            else {
                domScope.listaForm.usuario.$setValidity('duplicado',true);
            }
        }
    };
});
<!DOCTYPE html>
<html ng-app="app">
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <div class="users-list" ng-controller="menuCtrl">
            <ul ng-repeat="lista in listagem">
                <li>Nome: {{lista.nome_completo}}</li>
                <li>E-mail: {{lista.email}}</li>
                <li>Usuário: {{lista.usuario}}</li>     
            </ul>

            <form name="listaForm">
                <hr>
                <label>E-mail:</label>
                <input type="text" name="email" ng-model="lista.email" ng-change="verificarDuplicidadeEmail()" ng-required="true" ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/" />
                <code ng-show="listaForm.email.$error.required && listaForm.email.$dirty">Informe o e-mail!</code>
                <code ng-show="listaForm.email.$error.pattern">Informe um e-mail válido!</code>                                                                                    
                <code ng-show="listaForm.email.$error.duplicado">E-mail já Utilizado!</code>
                <hr>
                <label>Usuário:</label>
                <input type="text" name="usuario" ng-model="lista.usuario"   ng-required="true" ng-minlength="6" ng-maxlength="15" />
                <code ng-show="listaForm.usuario.$error.required && listaForm.usuario.$dirty">Informe o nome de usuário!</code>
                <code ng-show="listaForm.usuario.$error.minlength">O campo usuário deverá no mínimo 6 caracteres</code>
                <code ng-show="listaForm.usuario.$error.maxlength">O campo usuário deverá no mínimo 15 caracteres</code>                                                                                    
                <code ng-show="listaForm.usuario.$error.duplicado">Já existe usuário cadastrado</code>
                <hr>
                <button ng-disabled="listaForm.$invalid">
                    Salvar Alterações
                </button>
            </form>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
    </body>
</html>

1 answer

1

I researched and I found the answer. Help for you.

Function has been updated:

    $scope.verificarDuplicidade = function (email) {
    for (i in $scope.listagem) {
        if ($scope.listagem[i].email == email) {
            $scope.listaForm.email.$setValidity('duplicado',false);
        } else {
            $scope.listaForm.email.$setValidity('duplicado',true);
        }
    }    
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.