Solved

Name Field Validation

Posted on 2016-09-13
4
25 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service with UDP 2 28
Paging GridView 7 32
getting id from database 5 24
Calling stored proc in EDMX 11 17
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now