How to validate a zip using jQuery.validate

0

I'm not able to validate a zip code that the user has typed.

I added the following code inside the jquery.validete.js

jQuery.validator.addMethod("cep", function(value, element) {
  return this.optional(element) || /^[0-9]{2}.[0-9]{3}-[0-9]{3}$/.test(value);
}, "Por favor, digite um CEP válido");

But if I type: 11.111-111 it accepts, when in fact this zip code does not exist.

    
asked by anonymous 26.10.2015 / 18:31

2 answers

1

I will use the free webservice link

so it will return me a JSON link which I will save on an object.

Thanks @ Sanction.

    
27.10.2015 / 12:00
2

Change the regular expression to:

/^[0-9]{5}-[0-9]{3}$/

The value between braces defines the number of times the characters in brackets will repeat, so the validation of the value 11.111-11 with /^[0-9]{2}.[0-9]{3}-[0-9]{3}$/ is correct

    
26.10.2015 / 18:54