Domanda

I know there are lots of similar questions, but I couldn't find my case anywhere.

I'm trying to write a Full Name RegEx in Ruby on Rails user model. It should validate that first name and last name are filled with one whitespace. Both of the names should contain at least 2 characters (ex: Li Ma).

As a bonus, but not necessary I would like to trim the whitespaces to one character in case that user will mistype and enter more than one whitespace (ex: Li    Ma will be trimmed to Li Ma) Currently I'm validating it like that (Warning: It might be incorrect):

validates :name,
        presence: true,
        length: {
            maximum: 64,
            minimum: 5,
            message: 'must be a minimum: 5 letters and a maximum: 64 letters'},
        format: {
            # Full Name RegEx
            with: /[\w\-\']+([\s]+[\w\-\']){1}/
        }

This works for me, but doesn't check for minimum 2 characters for each name (ex: Peter P is now correct). This also accepts multiple whitespaces which is not good (ex: Peter    P)

I know that this problem of identifying names is very culture-centric and it might be not a proper way to validate full name (maybe there are people with one character name), but this is currently a requirement.

I don't want to split this field to 2 different fields First name and Last name as it will complicate user interface.

È stato utile?

Soluzione

You could match the following regex:

/([\w\-\']{2,})([\s]+)([\w\-\']{2,})/

and replace with: (assuming it supports capturing groups)

'\1 \3' or $1 $3 whatever the syntax is:

It gets rid of extra whitespaces and only keeps one, as you wanted.

Demo: http://regex101.com/r/oQ6aO7

Altri suggerimenti

result = subject.gsub(/\A(?=[\w' -]{5,64})([\w'-]{2,})([\s]{1})\s*?([\w'-]{2,})\Z/, '\1\2\3')

http://regex101.com/r/dT1fJ4

Assert position at the beginning of the string «^»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[\w' -]{5,64})»
   Match a single character present in the list below «[\w' -]{5,64}»
      Between 5 and 64 times, as many times as possible, giving back as needed (greedy) «{5,64}»
      A word character (letters, digits, and underscores) «\w»
      The character “'” «'»
      The character “ ” « »
      The character “-” «-»
Match the regular expression below and capture its match into backreference number 1 «([\w'-]{2,})»
   Match a single character present in the list below «[\w'-]{2,}»
      Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}»
      A word character (letters, digits, and underscores) «\w»
      The character “'” «'»
      The character “-” «-»
Match the regular expression below and capture its match into backreference number 2 «([\s]{1})»
   Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «[\s]{1}»
      Exactly 1 times «{1}»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the regular expression below and capture its match into backreference number 3 «([\w'-]{2,})»
   Match a single character present in the list below «[\w'-]{2,}»
      Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}»
      A word character (letters, digits, and underscores) «\w»
      The character “'” «'»
      The character “-” «-»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top