vcliment89
vcliment89

Reputation: 75

Regex person name comparision

I'm not very good with Regex and I was looking over the internet for this but couldn't find it. On my DB I'm storing first_name, second_name and last_name but on my website I only have two texboxes (One for first_name and the other for last_name). I want to include a checking to allow include first_name and second_name on the same textbox. So the checking should be:

is this possible using a Regex? Thanks, any idea will be welcome.

UPDATE

I found another approach using a JS library https://github.com/joshfraser/JavaScript-Name-Parser. I'll do some modifications:

Then I'll store the name separately on the DB, as this is a short register form I want as less textboxes as possible. Then the user will be able to go to his profile and edit the data separately if it's wrong.

Upvotes: 0

Views: 293

Answers (2)

nnnnnn
nnnnnn

Reputation: 150080

Something like this:

/^[A-Z']{1,50}(\s[A-Z'\s]{1,50})?$/i

(UPDATE: Note that although I believe the above JS regex matches your stated requirement, I'm not sure that your requirement matches with real-world names because you seem to be assuming that the first space marks the end of the first name. I have some friends with Asian names where their first name has a space in it. I also have a friend who spells her first name "Ann Marie" with a space rather than a hyphen; she also has a middle name, and in her case it would be incorrect to assue that "Marie" is part of the middle name.)

Upvotes: 2

user815512
user815512

Reputation: 194

While I think this is possible to do in regex I think it is easier and cleaner to split the task up a bit before using regex. I recommend you perform a split on the space character and then check the resulting string lengths and then with a regex ensure only letters and apostrophe.

Upvotes: 1

Related Questions