So I am writing a program that checks for an employee pay, the user enters a fullname (firstname, lastname) and then a number of pieces that needs to be processed, how would you validate that the full name is acceptable?

for example we enter in the name texbox the name "John Smith" ===> this is a valid username
and if we enter "John...... Smith" it is invalid
or "John" ======> invalid

How would I check if its only text? and two parts?

Thank you

Recommended Answers

All 9 Replies

See this Code Here the name of the textbox is name. If it contains anything other than letters it is invalid or more or less than 2 words it is invalid. But i dont think it is the best way..

Private Sub name_LostFocus(sender As Object, e As EventArgs) Handles name.LostFocus
    Dim nam As String = name.Text
    Dim sp As Integer
    For i = 0 To nam.Length - 1
        If (Asc(nam(i)) >= Asc("A") And Asc(nam(i)) <= Asc("Z")) Or (Asc(nam(i)) >= Asc("a") And Asc(nam(i)) >= Asc("z")) Then
            Continue For
        ElseIf nam(i) = " " And nam(i - 1) <> " " Then
            sp += 1
        Else
            sp = 2
        End If
    Next
    If sp = 1 Then
        'Valid Name
    Else
        'Invalid Name
    End If
End Sub

What do you consider valid text? If you limit it to only letters and blanks then names like Ryan O'Neal and Joseph Gordon-Levitt would both be invalid. Why not just allow any characters, then parse on a blank. If you get more than two names then disallow. If the two names don't match anything in the database then disallow.

No that wouldn't help.. The requirements that I have to use a text box and @PalashBansal96 this is not the correct way to do it.. It works correct but what if there is a change happened and you have to add a character to a name? Like $ or . so that won't work, @Reverend Jim you are correct, what if the name had many spaces and characters? But I still need to validate at least one first and last name more than two spaces between these will be invalid including a space at the begining.. Any other ideas?

You might be making it too complicated. You could simplify the rules and post them on the form(eg. only first and last name, only letters and numbers, only spaces, apostrophes, and dashes for punctuation), then simply use Split to separate the names and check if the name is in the list, if not get the user to follow the rules. If you start trying to account for every possibility of every format that a name can be, you'll probably end up pulling out your hair.

Hi,

Write this in Lost Focus, of the TextBox...

    Dim SpFlg As Boolean
    Dim i As Integer
    Dim TAsc As Integer
    Dim TChar As String
    Dim TNewText As String
    SpFlg = False
    TNewText = ""
    For i = 1 To Len(Text1.Text)
        TChar = Mid(Text1.Text, i, 1)
        TAsc = Asc(TChar)
        If Not SpFlg Then
            If TAsc = 32 Then
                SpFlg = True
            End If
            TNewText = TNewText & TChar
        Else
            If TAsc = 32 Then
            Else
                TNewText = TNewText & TChar
            End If
        End If
    Next
    Text1 = TNewText
    If Not SpFlg Then
        MsgBox ("Enter both first and last name..")
    End If

Multiple spaces.. are just clipped...

Regards
Veena

Well I think you just need to simplify it by using the textbox's for each variable you want, e.g. for name there will be a textbox for name as well as for surname. But I've worked it out using the same texbox. meaning there is only one texbox and the user will enter both name and surname than the validation you will just add it or refer it to where you want or to your database.

Imports System.Windows.Forms
Public Class Form1

Dim name, name1, name2, space As String
Dim spacekey As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer2.Start()
    Timer1.Start()

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    name2 = TextBox1.Text
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    If InStr(TextBox1.Text, " ") > 0 Then
        name1 = TextBox1.Text
        TextBox1.Text = ""
        Timer1.Stop()
    End If

    If Keys.Space = Keys.Space = True Then

    Else

    End If


End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If name1 = "mlungisi " And name2 = "ndlela" Then 'Note of the space on the variable in name1
        MsgBox("Your information is valid")
        name = name1 & " " & name2
    Else
        MsgBox("Invalid entry")
    End If

    TextBox1.Text = name

End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    name = name1 & " " & name2
    'TextBox1.Text = name
    'If TextBox1.Text = CanSelect = False Then
    'TextBox1.Text = name
    ' End If
    End Sub
End Class

`

What I did here was that I firstly separated the variables in to two 1 for name and other for surname so that it will also be easy for your to refer or validate then once the user is done typing I combined the words the same way as he/she was entering them then I redisplayed it so that it will all be visible, Take note of the timer I've stoped the timer once the user has hit a spacebar because that will say that the user is now typing the surname as we all know that the user will have to press space to separate the name from surname. NB: the name1 will be recorded with a space e.g. "mlungisi" will be "mlungisi " but if you will just tell people instand of pressing a spacebar and press e.g. Shift than that will make it serve your intentions but then it your choice.

With the Split function you can simplify it greatly. Something like this should work:

Public Class Form1

    Dim FirstName As String = ""
    Dim LastName As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Names() As String = Textbox1.Text.Split(" ")
        Select Case Names.Count
            Case 2
                FirstName = Names(0)
                LastName = Names(1)
                'Call a function here, passing the 2 names, to check if the names are present in the list, database, whatever
            Case 1
                MsgBox "Enter Both Names"
            Case Else
                MsgBox "Enter Only The First and Last Names and No Extra Spaces"
         End Select
     End Sub
 End Class
commented: Yours works Perfectly.. But there must not be a space at the start.. I put a space and it accepted it, then I put 2 the error threw, yours is the best for now but I will try to fix the space issue. +1

Why not just separate textboxes for first and last names?

commented: Best solution. (Not knowing the problem( If any) ) +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.