Rails validating full_name
Asked Answered
D

3

10

Hey... how would you validate a full_name field (name surname).

Diphthongize answered 13/4, 2010 at 12:23 Comment(3)
need some good reg for validates_format_ofDiphthongize
What are the acceptable values, and what are not? To my mind you can check only, that there should not be any special chars like #@%..., numbers and should be at least 1 space.Miskolc
Some people go by a single name, so even looking for a space can be problematic. Ask yourself: Why am I validating a name field? What problem are you trying to prevent from occurring?Regale
S
33

Consider names like:

  • Ms. Jan Levinson-Gould
  • Dr. Martin Luther King, Jr.
  • Brett d'Arras-d'Haudracey
  • Brüno

Instead of validating the characters that are there, you might just want to ensure some set of characters is not present.

For example:

class User < ActiveRecord::Base

  validates_format_of :full_name, :with => /\A[^0-9`!@#\$%\^&*+_=]+\z/
  # add any other characters you'd like to disallow inside the [ brackets ]
  # metacharacters [, \, ^, $, ., |, ?, *, +, (, and ) need to be escaped with a \

end

Tests

Ms. Jan Levinson-Gould         # pass
Dr. Martin Luther King, Jr.    # pass
Brett d'Arras-d'Haudracey      # pass
Brüno                          # pass
John Doe                       # pass
Mary-Jo Jane Sally Smith       # pass
Fatty Mc.Error$                # fail
FA!L                           # fail
#arold Newm@n                  # fail
N4m3 w1th Numb3r5              # fail

Regular expression explanation

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \A                       the beginning of the string
--------------------------------------------------------------------------------
  [^`!@#\$%\^&*+_=\d]+     any character except: '`', '!', '@', '#',
                           '\$', '%', '\^', '&', '*', '+', '_', '=',
                           digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  \z                       the end of the string
Superable answered 13/4, 2010 at 15:16 Comment(4)
I found this issue on Rails 4.0.1 The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option? But it was solved with adding :multiline => true validates_format_of :name, :with => /^[^0-9`!@#\$%\^&*+_=]+$/, :multiline => trueTabathatabb
@barek2k2, I should be using \A and \z here. Thanks for the comment.Guernsey
Bit late though... However keep in mind that this regex approves of the name being ------Prettypretty
Consider non-English speakers (names in Cyrillic etc.). It's a mess.Tupper
K
1

Any validation you perform here is likely to break down unless it is extremely general. For instance, enforcing a minimum length of 3 is probably about as reasonable as you can get without getting into the specifics of what is entered.

When you have names like "O'Malley" with an apostrophe, "Smith-Johnson" with a dash, "Andrés" with accented characters or extremely short names such as "Vo Ly" with virtually no characters at all, how do you validate without excluding legitimate cases? It's not easy.

Kneepan answered 13/4, 2010 at 14:10 Comment(0)
O
1

At least one space and at least 4 char (including the space)

\A(?=.* )[^0-9`!@#\\\$%\^&*\;+_=]{4,}\z
Owensby answered 27/9, 2014 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.