My guess is, you’re wanting to check the total length of what they
entered… As the length of their username must be (for example)
between 5 and 20 characters long?
If so, I’d say you’re making things needlessly complex.
min = 5
max = 20
user_name = params[:user_name]
return false unless(
(min…max).include?(user_name.length) # check size
and user_name =~ /^\w/ # must start
and user_name =~ /\w$/ # must end
and user_name !~ /\W\W/) # no two
I’ve actually been using rubular - but unfortunately it isn’t quite as
helpful for more advanced regexes involving lookaheads / behinds etc.
Although I found a proper regex that worked using Joel’s example, a
better solution to the problem was just to use my existing regex with
validates_format_of, in conjunction with validates_size_of
It’s worth noting that \w and \W do not handle utf-8 properly in all
cases, even with the u option. If you plan to allow/support i18n’ed
usernames, consider using Unicode character properties in you regular
expressions. For more information take a look at: