how to put a variable array inside the match() function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    how to put a variable array inside the match() function?

    Code:
    function zipcode_detect(){
    	
    	
    	var albay_zipcode = Array("4509","4502","4501","4503","4515","4500","4507","4504","4510","4512","4514","4504","4516","4506","4517","4508","4511","4513");
    
    var albay_town = Array("bacacay","camalig","daraga","guinobatan","jovellar","legazpi city","libon","ligao","malilipot","malinao","manito","oas","pio duran","polangui","rapu-rapu","sto. domingo","tabaco","tiwi");
    
    var ddf = document.form_zip.addrz.value;
    		
    		
    	for(var count = 0; count<18; count++ ){
    		var input = ddf.match(albay_town[count]);	
    		if(input){
    			alert('match '+albay_zipcode[count]);
    			
    		}else{
    			alert('not');	
    			
    		}
    	}
    }//end of function
    i want to check if the value of my textfield has a word match in my array albay_town widthout using the for loop.
    example input: camalig, albay;
    the output must be: match 4502;
    can you help me please. . . :)
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can use the indexOf method of the array class to look for the position matching a specified value.

    However, you will get no results in your example case because you're looking for "camalig, albay" and there is no such value in your array. You only have "camalig" in your array.

    Comment

    • Exequiel
      Contributor
      • Jul 2012
      • 288

      #3
      Code:
       function zipcode_detect(){
       
       
      var albay_zipcode = Array("4509","4502","4501","4503","4515","4500","4507","4504","4510","4512","4514","4504","4516","4506","4517","4508","4511","4513");
       
      var albay_town = Array("bacacay","camalig","daraga","guinobatan","jovellar","legazpi city","libon","ligao","malilipot","malinao","manito","oas","pio duran","polangui","rapu-rapu","sto. domingo","tabaco","tiwi");
       
      var ddf = document.form_zip.addrz.value;
      var input = ddf.toLowerCase();    
       
          for(var count = 0; count<18; count++ ){
       	if(input.match(albay_town[count])){
      		alert('match '+albay_zipcode[count]);
      		break;
      	}     
          }
      }//end of function
      I got it already. . . i used this codes

      Comment

      Working...