Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 62
  • Last Modified:

Name Field Validation

Hello There,

Please be kind enough to provide me with a code to best validate a name field with or without initials.
E.g - The name field should be represented like the following;

A. Buddhi
L. Natan
Kumar. S or
Jim
Bob
Kate

All the above tests should pass without showing an error message.

If the name ends with a dot or starts with a dot it should give a message as *Required
any other data should be prevented on key input.

Kind Regards,
Indunil Sanjeeewa
0
Indunil Sanjeewa Amarasinghe
Asked:
Indunil Sanjeewa Amarasinghe
  • 2
1 Solution
 
KimputerCommented:
Sorry that it's VB, but should help you enough on the way (probably takes you a few more minutes to get it to C#):

Imports System.Text.RegularExpressions

Public Class Form1

    Dim names() As String = {"A. Buddhi", "L. Natan", "Kumar. S", "Jim", "123", "AA. BDDF", "AA BB", "1. Cekke", "AAAAs. 3"}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each single_name In names
            TextBox1.AppendText(single_name & " = " & checkname(single_name) & vbCrLf)
        Next
    End Sub

    Function checkname(name As String)
        Dim regex As Regex = New Regex("^[A-Z].\s[A-Z][a-z]+") 'A. Buddhi = True, L.Natan = True
        Dim regex2 As Regex = New Regex("^[A-Z][a-z]+.\s[A-Z]$") 'Kumar. S = True
        Dim regex3 As Regex = New Regex("^[A-Z][a-z]+$") 'Jim = True
        Dim match As Match = regex.Match(name)
        If match.Success Then
            Console.WriteLine(match.Value)
            checkname = True
            Exit Function
        End If
        match = regex2.Match(name)
        If match.Success Then
            Console.WriteLine(match.Value)
            checkname = True
            Exit Function
        End If
        match = regex3.Match(name)
        If match.Success Then
            Console.WriteLine(match.Value)
            checkname = True
            Exit Function
        End If
        checkname = False
    End Function

End Class

Open in new window


In case you want it case INsensitive, I need to modify it slightly.
1
 
Indunil Sanjeewa AmarasingheAuthor Commented:
Hello There,

Please be kind enough to give a valid regex for Initials.

E.g :- A.P  or J.K There are no spaces in between the dot and the letter K. It should be valid for any initial.

Kind Regards,
Indunil Sanjeewa
0
 
KimputerCommented:
Here's the update. Please think about a way to notify the user what format you're expecting. You don't want them tearing their hairs off because you didn't explain it through a messagebox or other method what they input wrongly.

Imports System.Text.RegularExpressions

Public Class Form1

    Dim names() As String = {"A. Buddhi", "L. Natan", "Kumar. S", "Jim", "123", "AA. BDDF", "AA BB", "1. Cekke", "AAAAs. 3", "J.K"}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each single_name In names
            TextBox1.AppendText(single_name & " = " & checkname(single_name) & vbCrLf)
        Next
    End Sub

    Function checkname(name As String)
        Dim regex As Regex = New Regex("^[A-Z].\s[A-Z][a-z]+") 'A. Buddhi = True, L.Natan = True
        Dim regex2 As Regex = New Regex("^[A-Z][a-z]+.\s[A-Z]$") 'Kumar. S = True
        Dim regex3 As Regex = New Regex("^[A-Z][a-z]+$") 'Jim = True
        Dim regex4 As Regex = New Regex("^[A-Z].[A-Z]$") 'J.K = True
        Dim match As Match = regex.Match(name)
        If match.Success Then
            Console.WriteLine(match.Value)
            checkname = True
            Exit Function
        End If
        match = regex2.Match(name)
        If match.Success Then
            Console.WriteLine(match.Value)
            checkname = True
            Exit Function
        End If
        match = regex3.Match(name)
        If match.Success Then
            Console.WriteLine(match.Value)
            checkname = True
            Exit Function
        End If
        match = regex4.Match(name)
        If match.Success Then
            Console.WriteLine(match.Value)
            checkname = True
            Exit Function
        End If
        checkname = False
    End Function

End Class

Open in new window

1

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now