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:
[0-9]{5}-[0-9]{4}
(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:
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.
[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]
I always go to http://www.regxlib.com/ 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.