Jquery Zip code service area verification woes. -
i'm trying validate form takes in users zipcodes , tests against array of zip codes serviced. i'd make work on submit. right nothing , throws no errors.. i'm lost. i'm kind of new jquery not programming
<doctype!> <html> <head> <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <script> var zipcodearray = ["98001", "98002", "98003", "98004"]; $("#zipcode").live('keyup', function(){ var zipcode = $(this).val(); if(zipcode.length >4){ if($.inarray(zipcode, zipcodearray)){ //do nothing, or whatever want //we have return true here. }else{ alert("sorry. offer services western washington @ time."); } } }); </script> </head> <body> <div class="zipcode"> <form> <input id="zipcode" name="zipcode" /> <input type="submit" value="submit"> </form> </div> </body> </html>
change
if($.inarray(zipcode, zipcodearray)){
to
if($.inarray(zipcode, zipcodearray) > -1){
$.inarray()
method returns index of element in array , not boolean value..
also need encase code in dom ready handler, script before content
$(function() { // code here });
Comments
Post a Comment