regex fails

Hi all,

I have the following line in my model:

validates_format_of :postal_code, :with => /\d{4}\s*[a-zA-Z]{2}/, :on => :create

However this still does allow me to create a postal code such as:

5444 ABC

which is incorrect, because it should only be allowed if two characters are supplied at the end.

In java the same regex is working, what am I doing wrong here?



Add ^ at the beginning and $ - at the end:

validates_format_of :postal_code, :with => /^\d{4}\s*[a-zA-Z]{2}$/, :on
=> :create

Your expression looks fine, but you can’t expect all parsers to behave identically. You might try doing [a-zA-Z][a-zA-Z].


Jose Hales-Garcia

UCLA Department of Statistics

Thanks Maxim,

That seemed to be it!