Solved

Name Field Validation

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem of RegEx to match the first occurence of 10 36
Filename to be appended with DateTimeof Download 3 47
Error in JQuery 5 39
Variable Event ? 3 23
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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

15 Experts available now in Live!

Get 1:1 Help Now