Solved

Name Field Validation

Posted on 2016-09-13
4
38 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
  • 2
4 Comments
 
LVL 35

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 35

Accepted Solution

by:
Kimputer earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 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