Title |
Test
Find
First Name(s) OR Last Name(s)
|
Expression |
^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$ |
Description |
I wrote this regex to validate first names or last names separately. Allows for 2 names separated by space or hyphen. Will accept an abbreviated second name with period. Has to start with a letter and end with a letter or capital letter with period.
Updated 3/24/2005: Now, names like McDonald work. Also, several convenient parts of the match are stored, for example: the Name MacDonald-O'Brian will yield the following: $1="MacDonald" $2="ac" $3="-" $4="O'Brian" $5="'" |
Matches |
O'Grady-Smith | Rachel Ann | McDonald T. |
Non-Matches |
Timothy J | James Earl Jones | @#$!*! |
Author |
Rating:
Not yet rated.
Timothy Dutton
|
Source |
|
Your Rating |
|
Title: Doesn't work
Name: MG
Date: 3/31/2005 11:53:25 AM
Comment:
I tested this using the following code... It returns false when using Smith-Jones or St. Louis...
Private Sub ValidateName(ByVal ctl As UltraTextEditor)
Dim _regexpattern As String = "^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$"
If System.Text.RegularExpressions.Regex.Match(ctl.Text, _regexpattern).Success = False Then
ctl.Focus()
ctl.Select(0, ctl.Text.Length)
Throw New ValidationException("Name is required")
End If
End Sub
Title: Fixed for some names that did not match
Name: Timothy Dutton
Date: 3/24/2005 12:24:06 PM
Comment:
McDonald, MacDonald, DeHoyas, etc. now work
Title: Doesn't understand some names as being correct
Name: Jay
Date: 3/22/2005 2:01:37 PM
Comment:
I seem to have a problem with names beginning with Mc, such as McDonald