How to validate zip code with Angularjs?

Asked

Viewed 884 times

0

I have an application where I use zip code to be part of the registry. Only the CEP is enough and would need to be validated the CEP and at the end of the validation trigger a function. How can I do that?

html:

<input type="text" ng-model="endereco.cep" ng-keyup="pegaCep()" placeholder="CEP">

angular:

$scope.pegaCep = function () {
    var cep = $scope.endereco.cep;
    if(cep.length <= 8){
        return
    }else
    if(cep.length == 9){
        $http.get("http://www.vigilantescomunitarios.com/www/php/pegaCep.php?cep="+$scope.endereco.cep).success(function (endereco){

            $scope.endereco = endereco;
        });
    }
 }
  • It would have as [Edit] your post and add the code you are using, so we can better analyze and suggest where to apply the change. Thank you.

  • I was able to do the validation, now I just need to make the function trigger when I finish typing the 9 digits.

  • The function you say is to fill in the inputs with the address data?

  • No, only the zip code. After the 9 characters of the zip code have been filled in, it triggers a function that fetches the other zip code data. What I have today, is, when leaving the zip code field, it searches the data, got @durtto?

  • If the zip code is wrong, you issue a warning that is wrong?

  • No, that I’m not validating.

  • I got it. I put my solution in the post description.

  • The ideal would be for you to use a Directive validation that would receive as an argument a callback to be called when the field was valid.

  • How could I do this @Zignd?

Show 4 more comments
No answers

Browser other questions tagged

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