Forum: Ruby on Rails Validating no spaces in field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-03-12 15:54
(Received via mailing list)
All,

I've never been much of a regex wrangler, and learning rails at the
same time isn't helping.

I want to create a validation that fails if a user tries to create a
username with spaces.  But I can't seem to get the right
validates_format_of regex going.

Can anyone help with this simple question?

Thanks!
Andrew
sayoyo (Guest)
on 2007-03-12 17:46
Hi,

you can simply try yourString.include?(" ") with the params[:yourString]
if it is  a form parameter.

Sayoyo



removed_email_address@domain.invalid wrote:
> All,
>
> I've never been much of a regex wrangler, and learning rails at the
> same time isn't helping.
>
> I want to create a validation that fails if a user tries to create a
> username with spaces.  But I can't seem to get the right
> validates_format_of regex going.
>
> Can anyone help with this simple question?
>
> Thanks!
> Andrew
Eden L. (Guest)
on 2007-03-12 19:28
(Received via mailing list)
validates_format_of :username, :with => /^\S+$/

but that's still probably too open for a username (it'll allow "u
$ern4m3" for instance)

Most of the time, usernames are restricted to:

validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]{2,10}$/

(usernames starting with a letter, followed by 2 or more letters or
digits up to 10, min username length = 3, max username length = 11)

here are a few links that explain the syntax concisely:
 - http://www.math.hokudai.ac.jp/~gotoken/ruby/ruby-u...
 - http://www.rubyist.net/~slagell/ruby/regexp.html

On Mar 12, 9:54 pm, "removed_email_address@domain.invalid"
(Guest)
on 2007-03-13 03:07
(Received via mailing list)
Thank you so much, that did the trick perfectly!

And double thanks for teaching a man to fish.  I think I might be able
to write a simple regex of my own now!

Thanks again!!!

---A
Russell N. (Guest)
on 2007-03-13 13:36
(Received via mailing list)
While I think that your regex is smokin' hot, I do think you might be
better
served [at least on the error messages front] by two validations there:

validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]*$/,
:message
=> "can only contain letters and numbers" # Or something
validates_length_of, :minimum => 2, :maximum => 10, :too_short => "must
be
at least 2 characters", :too_long => "cannot be longer than 10
characters"

that way you'll have separate error messages for each problem rather
than
having to lump them all in together. You way _does_ work though. I'm
just
trying to offer a helpful hint.

RSL

On 3/12/07, eden li <removed_email_address@domain.invalid> wrote:

validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]{2,10}$/
(Guest)
on 2007-03-13 18:46
(Received via mailing list)
Russell,

I agree and my model already had a validates_length_of so I modified
the proposed regex to eliminate the length check.

Discussing this with my partner we decided to allow numbers, letters,
and underscore (_) so I am going to try the regex
/^[a-zA-Z0-9_]*$/

We don't care if the first character is a letter, number, or
underscore, only that these are the only characters allowed anywhere
in the username.  So "_username", "user_name", and "1user_name" should
all be valid.

Think this should work?  I don't have access to the site to test it
until I get home tonight.

I had never noticed that validates_length_of supports two error
messages, one for too short, and one for too long.  I'll have to
implement that in my code tonight as well.

Now if I could only find a non-ugly way to sort the error messages so
they match the order of the form fields I'd be all set. :(
Russell N. (Guest)
on 2007-03-13 19:31
(Received via mailing list)
It's not [that] hard to roll your own version of error_messages_on.
Something like

def error_messages_hacked(instance, *order)
  return if instance.errors.empty?
  dummy = order.map do |attribute|
    "#{instance} #{instance.errors_on(attribute)}"
  end.flatten
  # Do any HTML styling you want on dummy
  # which will be a collection of error message strings
end

<%= error_messages_hacked @user, :name, :email, :whatever -%>

That's pretty not ugly, right?
This topic is locked and can not be replied to.