How to do zip validation for Android

3

I would like to know how do I validate a user-entered zip in an EditText on Android. Do I need any specific APIs? I want to validate if the zip is existing and return to the screen if it is not.

    
asked by anonymous 15.05.2015 / 16:08

1 answer

1

If you have the formula for validating the zip code at hand, you can do this in the class.

Capture your text using

TextView textoCEP = (TextView) rootView.findViewById(R.id.<suaEditText>);

Then, put in a String that text capturing it with a <sua editText>.getText(); You may need to use .toString(); because they usually return CharSequences .

If it's not that, it's something along those lines. I hope I can help you.

EDIT: So Validating ZIP codes is a bit tricky. You can check if the zip has the right number of digits, which are 8, and format it using a substring (Seen on the internet / not tested):

        if (cep.Length == 8) {
            cep = cep.Substring(0, 5) + "-" + cep.Substring(5, 3);
        }

However, the final three digits indicate the state from where that ZIP code comes from. As it would be great to paste all the numbers here, it follows something similar in a forum that I saw in Google. It's not in Java, but you should get the idea: link

    
15.05.2015 / 16:47