I am trying to write a regex which will validate the format of a string to match “AAAAA9999A”. The first five digits are “A-Za-z” and the next 4 digits are numbers, with the last digit “A-Za-z”.
For this, I wrote my regex as [A-Za-z]{5}\d{4}[A-Za-z]{1}
Of course this is not working.
validates_format_of :pan_number, :with => [A-Za-z]{5}\d{4}[A-Za-z]{1}, :message => 'is not in a format of AAAAA9999A'
What am I doing wrong?
Additionally, I want the fourth character to be one of C, P, H, F, A, T, B, L, J, G instead of A-Z.
You’ll want to write it out as an actual regex (that is, you missed the /’s around the expression).
Try with this line:
This also validates the fourth character as one of C, P, H, F, A, T, B, L, J, G, and it uses the case insensitive flag (
/i) on the regex, so you don’t have to write[A-Za-z].