Help with jQuery validate and a Zip code restriction based on the 1st three digitss -


i assuming need regular expression here?

i have zip code field , zip codes must in city limits of chicago. luckily zip codes begin 606. need poll input make sure zip code entered 5 digits , begin numbers 606:

my input basic enough:

<label for="dzip"><span class="red">&hearts;&nbsp;</span>zip:</label> <input name="attributes[address_zip]" id="dzip" type="text" size="30" class="required zip-code" /> 

and script city easy enough. need apply zip-code well:

    jquery.validator.addmethod("chicago", function(value) {     return value == "chicago"; }, '**recipient must reside in chicago city limits**'); 

may if show how plug-in functions phone-numbers (us). need translate zips:

    jquery.validator.addmethod("phoneus", function(phone_number, element) {     phone_number = phone_number.replace(/\s+/g, "");     return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/); }, "please specify valid phone number"); 

what part do/mean?:

phone_number.replace(/\s+/g, "") 

i dropped in phone part directly example on validate site.

final answer great (and quick) input here this:

    jquery.validator.addmethod("zip-code", function(zip_code, element) {     zip_code = zip_code.replace(/\s+/g, "");     return this.optional(element) || zip_code.length == 5 && zip_code.match(^606[0-9]{2}$); }, "please specify city of chicago zip code"); 

for length use .length property

if($('#dzip').val().length != 5)   return false; 

for 606 use substr method

if($('#dzip').val().substr(0,3) != 606)   return false; 

combined

$('#dzip').change(function(){   var val = $(this).val();   if(val.length != 5 || val.substr(0,3) != 606)     return false; //not valid   else   //do stuff here }) 


try this:

jquery.validator.addmethod("chicago", function(zip, element) {     zip = zip.replace(/\s+/g, "");     return this.optional(element) || zip.match("^606[0-9]{2}$"); }, '**recipient must reside in chicago city limits**'); 

the above code can read as

  • add method chicago validator plugin
  • when method called strip whitespace characters
  • then if field not required , empty
  • or if required , not empty , 5 digits in length
  • and first 3 characters 606
  • return true
  • else return false

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -