Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Name Field Validation

Posted on 2016-09-13
4
Medium Priority
?
59 Views
Last Modified: 2016-10-30
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
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Kimputer
ID: 41795928
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
 

Author Comment

by:Indunil Sanjeewa Amarasinghe
ID: 41796011
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
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 total points
ID: 41797620
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question