2

I am trying to set a regex validation on a form with the code below. I want to allow any alphabetical character, including accents, numbers and hyphen, apostrophe, comma and space. This expression should match the result : "Tir à l'arc, 3d, danse"

validates :interest_list, tags: true, if: lambda { interest_list.any? }
  validates :interest_list, format: { with: /\A[[:alpha:]\d-'’, ]\z/, message: "only allows letters, space, hyphen and apostrophe" }

But I have this error empty range in char class: /\A[[:alpha:]\d-'’,]\z/

Can anyone tell me what I'm doing wrong ?

3
  • because you have non-pure latin character à in it Commented Apr 26, 2016 at 16:00
  • à may contain two chars alpha and accent Commented Apr 26, 2016 at 16:06
  • The range error seemed to come from the - character who was not escaped/placed at the beginning or the end. Commented Apr 26, 2016 at 17:14

2 Answers 2

6

Any - that appears inside a character class in any position other than the first or last is treated as a range, ie. [0-9] is shorthand for [0123456789]. This range is calculated based on the ASCII values.

You have \d-' in your regex and \d isn't valid to use for the start/end of a range. Probably what you want is to move - to the start or end of your []

/\A[[:alpha:]\d'’, -]\z/

...and to solve your next problem/question - as it is your regex will only match a single character, you probably also want a repeat on that character class, like a +:

/\A[[:alpha:]\d'’, -]+\z/
Sign up to request clarification or add additional context in comments.

1 Comment

You got the acceptance because you replied to my second question :) Thanks
0
Error: Regex Construction .. 

Invalid range end in character class

\A[[:alpha:]\d->>>HERE>>>'’, ]\z  

\d - anything is invalid range because a range operator - cannot
specify a range between a class and anything else.

You'd need to escape the - to make it a literal \A[[:alpha:]\d\-'’, ]\z

or add it to the end or beginning \A[[:alpha:]\d'’, -]\z

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.