Android Question [SOLVED] Full name field Validation

ProjectGroup19

Active Member
Licensed User
Greetings Everyone.

Please, I need help on how to validate a text field which accepts users full name.

The field should only be valid if it is similar to the following.
Eg. Isaac Donkor, Michael John Smith, etc.


Thank you in advance for the help.
 
Solution
There is also a way to capitalize the first letter of each of the words
B4X:
    Log("*** Fixed format First Middle Last ***")
   
    Log(ToMixCase(B4XFloatTextField1.Text))
   
    If Regex.IsMatch("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text) Then
        Dim Matcher1 As Matcher = Regex.Matcher("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text)
        Matcher1.Find
        Log("First: " & ToMixCase(Matcher1.Group(1)))
        Log("Middle: " & ToMixCase(Matcher1.Group(2)))
        Log("Last: " & ToMixCase(Matcher1.Group(3)))
    End If

B4X:
Public Sub ToMixCase(Entry As String) As String
    Dim sb As StringBuilder
    Dim I As Int
    Entry = Entry.ToLowerCase
    sb.Initialize
    Dim m As Matcher =...

Brian Dean

Well-Known Member
Licensed User
Longtime User
I think that you are setting a very difficult challenge. Would the following names (all real people) be acceptable or not?

William McAlpine
Ron DeSantis
D'Angelo Russell

There are examples of Regex name validators on the Net, but I have never found one that can be guaranteed to accept every valid name and at the same time spot all invalid ones.

Anyway, I think that you need to provide a better definition of what you consider an acceptable name format.
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I guess by TextField in B4A I meant edittext....
B4X:
Public Sub EdiText1_EnterPressed
    Dim Text1 As EditText = Sender
    If Text1.Text.Trim.IndexOf(" ")>-1 Then
        ' accept
        ' focus to nex view
    Else
        ' wrong
        ToastMessageShow("Ahi ahi ahi",True)
        Text1.RequestFocus
    End If
End Sub

or
B4X:
Public Sub EdiText1_EnterPressed
    Dim Text1 As EditText = Sender
    If Text1.Text.Trim.Contains("Isaac Donkor,") Or Text1.Text.Trim.Contains("Michael John Smith")  Or Text1.Text.Trim.Contains("William McAlpine")  Then
        ' accept
        ' focus to nex view
    Else
        ' wrong
        ToastMessageShow("Ahi ahi ahi",True)
        Text1.RequestFocus
    End If
End Sub
 
Upvote 0

ProjectGroup19

Active Member
Licensed User
Please, what I was trying to emphasize on was the format. {First name} {Lastname} thus the Isaac Donkor. And {first name} {middle name} {last name} thus the Michael John Smith.
 
Upvote 0

ProjectGroup19

Active Member
Licensed User
I guess by TextField in B4A I meant edittext....
B4X:
Public Sub EdiText1_EnterPressed
    Dim Text1 As EditText = Sender
    If Text1.Text.Trim.IndexOf(" ")>-1 Then
        ' accept
        ' focus to nex view
    Else
        ' wrong
        ToastMessageShow("Ahi ahi ahi",True)
        Text1.RequestFocus
    End If
End Sub
Thank you I will try if this works.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
I think Regex should work.
Another workaround is to use a different textbox for first name and another for last name (3rd for a middle name that is if needed). You can then join them together. Or just check if the input string contains space.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Please, what I was trying to emphasize on was the format. {First name} {Lastname} thus the Isaac Donkor

Okay, that is a better definition. But in my case my surname (Dean) is also acceptable as a given name (Americans very often assume that it is my first name). In fact there may very well be somebody called "Dean Brian" reading this right now. And in some countries the family name (the patronymic) precedes the given name.

And what about ...

George Michael - British singer
Michael Douglas - American film actor
Douglas George - resident of Great Falls, Montana
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
simple test.

Validate the entry by separating the names and surnames

B4X:
Private Sub B4XFloatTextField1_EnterPressed
    Dim Matcher1 As Matcher = Regex.Matcher("([^\s]+)", B4XFloatTextField1.Text)
    Do While Matcher1.Find
        Log("Found: " & Matcher1.Match)
    Loop
End Sub
1638729516658.png


read:
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
other:
B4X:
Private Sub B4XFloatTextField1_EnterPressed
    
    Log("*** Free format of names and surnames ***")
    
    Dim Matcher1 As Matcher = Regex.Matcher("([^\s]+)", B4XFloatTextField1.Text)
    Do While Matcher1.Find
        Log("Found: " & Matcher1.Match)
    Loop
    
    Log("*** Fixed format First Middle Last ***")
    
    If Regex.IsMatch("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text) Then
        Dim Matcher1 As Matcher = Regex.Matcher("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text)
        Matcher1.Find
        Log("First: " & Matcher1.Group(1))
        Log("Middle: " & Matcher1.Group(2))
        Log("Last: " & Matcher1.Group(3))
    End If
End Sub
1638731895437.png

1638731961640.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
This won't work for Norah Mary O'Donnell
The free format will probably be more suitable in that case.
There is also a way to capitalize the first letter of each of the words via RegeX that escapes me,, although a sub can be built to do it.
You can add that in groups the characters (- or ') are part of the first or last name.

sample:
D'Angelo Russell
Norah Mary O'Donnell
Scarlett O'Hara
Catherine Zeta-Jones
etc.

Also, what I posted is just an example of what can be done and how to use regex.

The rest is on your own to find out how to add more conditions.

Sorry.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
There is also a way to capitalize the first letter of each of the words
B4X:
    Log("*** Fixed format First Middle Last ***")
   
    Log(ToMixCase(B4XFloatTextField1.Text))
   
    If Regex.IsMatch("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text) Then
        Dim Matcher1 As Matcher = Regex.Matcher("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text)
        Matcher1.Find
        Log("First: " & ToMixCase(Matcher1.Group(1)))
        Log("Middle: " & ToMixCase(Matcher1.Group(2)))
        Log("Last: " & ToMixCase(Matcher1.Group(3)))
    End If

B4X:
Public Sub ToMixCase(Entry As String) As String
    Dim sb As StringBuilder
    Dim I As Int
    Entry = Entry.ToLowerCase
    sb.Initialize
    Dim m As Matcher = Regex.Matcher("(^\w)|(\s\w)", Entry)
    Do While m.Find
        If m.Match.Length > 1 Then
            sb.Append(Entry.SubString2(I, m.GetStart(0) + 1))
            sb.Append(m.Match.SubString(1).ToUpperCase)
        Else
            sb.Append(Entry.SubString2(I, m.GetStart(0)))
            sb.Append(m.Match.ToUpperCase)
        End If
        I = m.GetEnd(0)
    Loop
    If I < Entry.Length Then
        sb.Append(Entry.SubString(I))
    End If
    Return sb.ToString
End Sub

1638736171828.png
 
Upvote 1
Solution

ProjectGroup19

Active Member
Licensed User
B4X:
    Log("*** Fixed format First Middle Last ***")
  
    Log(ToMixCase(B4XFloatTextField1.Text))
  
    If Regex.IsMatch("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text) Then
        Dim Matcher1 As Matcher = Regex.Matcher("^([\w-.]+)\h*(.+)\h*\b(\w+)$", B4XFloatTextField1.Text)
        Matcher1.Find
        Log("First: " & ToMixCase(Matcher1.Group(1)))
        Log("Middle: " & ToMixCase(Matcher1.Group(2)))
        Log("Last: " & ToMixCase(Matcher1.Group(3)))
    End If

B4X:
Public Sub ToMixCase(Entry As String) As String
    Dim sb As StringBuilder
    Dim I As Int
    Entry = Entry.ToLowerCase
    sb.Initialize
    Dim m As Matcher = Regex.Matcher("(^\w)|(\s\w)", Entry)
    Do While m.Find
        If m.Match.Length > 1 Then
            sb.Append(Entry.SubString2(I, m.GetStart(0) + 1))
            sb.Append(m.Match.SubString(1).ToUpperCase)
        Else
            sb.Append(Entry.SubString2(I, m.GetStart(0)))
            sb.Append(m.Match.ToUpperCase)
        End If
        I = m.GetEnd(0)
    Loop
    If I < Entry.Length Then
        sb.Append(Entry.SubString(I))
    End If
    Return sb.ToString
End Sub

View attachment 122706
This worked Great. Thank you.
 
Upvote 0
Top