Check if a value is inside an array

Asked

Viewed 2,635 times

2

would like to do the following: User inserts their zip code and a field input and the value that he entered within this field wanted to compare whether it is in the CEP range (01000 to 19999) of the state of SP.

My doubts:

1st - In case I would have to assemble an array with the numbers from 01000 to 19999?

2nd - How would I know if the value of inserted in input contains in the array?

1 answer

5


JS

var cep = '13426000';

if(arrayCeps.indexOf(cep) > -1){
   console.log('Tem no Array');
}

jQuery

if($.inArray(cep, ArrayCeps) > -1){
   console.log('Tem no Array');
}

PHP

With in_array($valor, $array) you check value.

$cep = '13426000';

if(in_array($cep, $arrayCeps){
   echo "Tem no Array";
}

With the array_key_exists($chave, $array) key check.

  • Thanks for the expensive reply, but I have to do all this in javascript/jquery, I can’t use PHP. :(

  • Sorry, I didn’t notice the tag. I’ll edit.

  • Ball show, I did a test here and it worked really thank you! Regarding the first question, I will have to list all the CEP’s in the array or have to compare only by declaring the start and end of the track?

  • You have to put them all in the Array. But you don’t have a database, a table with the ceps ?

  • I can not access through the mail api this track, if I do not have to do manually, but anyway thanks already helped a lot!

  • 1

    I found a very interesting site that will help me! https://viacep.com.br/

Show 1 more comment

Browser other questions tagged

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