Auto fill a zip code

Asked

Viewed 39 times

-2

Hello I’m having a problem with the zip code. When publish the Zip Code that had 0 was thrown wrong. Example: 03359005 was released 3359005. Within a span with cep class.

<span class="cep">3358140</span>

I’d like to know if you can automatically fill in a javascript, auto fill in the eight numbers with 0 to the left. Note: I already use the mask to put the stitch and trace:

<script type="text/javascript">
           
    
 $(document).ready(function () { 
        var $seuCampofone = $(".cep");
        $seuCampofone.mask('000.00-000');
    });
    </script>
  • I suggest trying to explain the problem better. How is this cep being set, ie, where is it coming from? Or is it static? Is this mask in the span? What library is being used to make this mask?

  • PS: this is not the mask that is usually used in ceps. In general we use: 00.000-00, or simpler: 00000-000

1 answer

-1

This seems very much to be the case if the cep comes from a numerical variable. If this is the case, remember that there are no numbers starting with 0 (except 0, obviously). That is, if you have a Cpf that comes in number format, there will be zeros before. There are two options:

  • The easiest is to bring the zip in string format, rather than numbers. I don’t see why not bring it, but you may not be able/can.
  • Option number 2 would be to convert the cep to string and add the missing zeros. Something like:

const cep = 3359005;
let cep_string = cep.toString();
while (cep_string.length < 8) {
  cep_string = '0' + cep_string
}

Browser other questions tagged

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