Check if a value is inside an array

2

I would like to do the following: The user enters his zip code and a input field and the value that he inserts into this field wanted to compare if he is in the zip code range (01000 to 19999) p>

My questions:

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

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

    
asked by anonymous 23.12.2016 / 16:35

1 answer

4

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 array_key_exists($chave, $array) checks keys.

    
23.12.2016 / 16:38