Clear Zip with JavaScript

7

How to clear the formatting of a ZIP code field in this format: 87.678-000 ?

I need to remove the "." and the "-" and return only the numbers. I tried something like that, unfortunately it did not work out.

var i = '^[0-9]+$';
var reg = new RegExp(i);
    
asked by anonymous 23.05.2014 / 18:47

3 answers

5

An alternative to the @Renan solution is to use this regular expression /\.|\-/ to remove the . and - characters from a string:

var cep = '87.678-000';
cep = cep.replace(/\.|\-/g, '');

alert(cep); // 87678000

Where the modifier /g is used to search for one or more occurrences where . and - is found, of global use.

Demo

To verify that a ZIP code is valid ( in the format provided by you ) something like this could be used:

var cep = '87.678-000';
if (/\d{2}\.\d{3}\-\d{3}/.test(cep))
    alert('Cep é válido!');
else
    alert('Cep inválido!');

Demo

    
24.05.2014 / 03:09
15

If all you want is to remove the dashes and periods ...

var str = str.replace("-", "").replace(".", "");

Where str is the string with the zip code.

If you want to see if a string is a valid ZIP code, you can use something like:

// força bruta não, força brutíssima! // após 3 edições, já não tão bruta...
var regexp = /\d\d((\d\d\d)|(\.\d\d\d-))\d\d\d/;
var match = str.match(regexp);

Then you check for match (if match is null, the string is not a valid ZIP). This regular expression will take both formatted and unformatted zip codes.

Q: I am not considering whether zip codes with leading zeros are valid, as I do not know the rules of CEP training.

Editing: I gave some code to make it less rough.

    
23.05.2014 / 18:53
1

I would like to complement the response of @Renan with a genuine validation.

Although the Brazilian CEP is not defined by an algorithm itself, but only one Structured decimal representation of Country , as any logical structure it follows a standard validable by Regular Expressions.

Note that I have written Regular Expressions , in the plural, since we have 26 states + a Federal District that, in one structure, result in 27 different standards.

Years ago, when I was searching for alternatives to autocomplete information based on a zip code, in an "era" where there were not many WebServices available and / or trusted, I ended up finding the database itself used by the Post Office to download in a blog that no longer exists.

The important thing is that amidst the comments of the article there was one of none other than Aurélio Marinho Jargas , author of the best reference book for Expressions Regular in Portuguese.

And in this particular comment he published these 27 Regular Expressions which I quickly dealt with implementing in PHP. Calm, cocada ...

Despite the relative difficulty I had in porting the code to JavaScript, I did, and I leave it as a reference for community in JSFiddle .

After the basic length check, the State is first checked by a Regular Expression apart. Only then did we test the first five digits of the CEP, which are the ones that matter against the Regular Expression of the informed state.

All credit goes to Master Aurelius, I just packed everything like a good Padawan. ^ _ ^

    
24.05.2014 / 14:56