Zip Code validation

Hello , I have required to Zip Code validation for ' 12345-6789 ' .I did not found regular experssion for this validation anywhere . If anyone have this , then please send me .
5 answers

It's not entirely clear to me what you exactly want between the format with and without the dash. If you just wanted to validate the format with the dash, you could just use the following regular expression:


(Which is just a shorter notation for Erwin's regex which should also work)

If you want to somehow convert an input without a dash to one with a dash, this sounds like a separate step to me:

  1. You could first verify that you have a string of 9 numbers ([0-9]{9}) and then insert a dash using substring calls.
  2. You could also first insert a dash by splitting the initial string using substring and inserting the dash, and then validate using the regex above.

If you somehow want to use an input field without a dash (and store this) and yet display it elsewhere with a dash, you would be best off just using two attributes.

To experiment with regexes, RegExr is also a great tool.




I always go to Most of the time somebody else already made a good regular expression.

Regards, Ronald


0 Vote down Add to favorites

Hi guys,

Zip Code is commonly 5 digit or else it must be 9 digits value with the format 99999-9999.

The attribute length given 10 in the validations.

In the text box user can enter 12345 (length is 5), 12345-6789 (length is 10 but shouldn't allow user to enter 10 digits , only - is accepted after 5 digits ), 123456789 (length is 9).

here the problem its allowing user to enter 10 digits (of course because the zip length is 10) , How to restrict the user entering 10 digits.


Perhaps an on change event on the field in combination with a regular expression like ^[0-9]{5}(-{0,1}[0-9]{4})?$ is what you want? In this case the user can enter either 12345, 123456789 or 12345-6789.