Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0


Wrote a function to validate pan card. Pan should 5 characters followed by 4 numeric and a character. I am validating 4 position but I want show error message   for test4 
--- Testpan(PanNo) calling function

Function TestPan(PAN As String) As Boolean Dim Pos As Integer Dim Test As Boolean Dim Passed As Boolean

Passed = True

For Pos = 1 To Len(PAN)
    Select Case Pos
        Case 1 To 3
            Test = TestForAlpha(Mid(PAN, Pos, 1))
        Case 4
            Test4 = TestForAlpha4(Mid(PAN, Pos, 1)) -------- here case4 
        Case 6 To 9
            Test = TestForNumeric(Mid(PAN, Pos, 1))
        Case 10
            Test = TestForAlpha(Mid(PAN, Pos, 1))
    End Select

    If Not Test Or Len(txtItPAN) <> 10 Then
        MsgBox "PAN numbers need to have 5 characters followed by 4 numbers then a final character. Please check your entry and try again"
        Passed = False
        Exit For
    End If
Next Pos

TestPan = Passed
End Function 

<pre>Function TestForAlpha(char As String) As Boolean
    If char >= "A" And char <= "z" Then
        TestForAlpha = True
    Else
        TestForAlpha = False
    End If
End Function

Function TestForAlpha4(char As String) As Boolean   -------- for this i want add else part
    If char = "A" Or char = "B" Or char = "C" Or char = "F" Or char = "G" Or char = "H" Or char = "L" Or char = "J" Or char = "P" Or char = "T" Then
        TestForAlpha4 = True
    Else
        TestForAlpha4 = False
    End If
End Function

Function TestForNumeric(char As String) As Boolean
    If Val(char) > 0 Then
        TestForNumeric = True
    Else
        TestForNumeric = False
    End If
End Function




--I want to add if else condition for case4 Test4 so that I can show error message for test4 position

What I have tried:

I am able validate pan card 5 characters followed by 4 numeric and a character


I want to know how to add else part case4 condition show that i can show error message
Posted
Updated 31-May-22 20:31pm
v2

You can use Regular Expressions in VB6: Regular Expressions in Visual Basic 6 - VB RegExp[^]
Then it's pretty simple:
^[a-zA-Z]{5}\d{4}[a-zA-Z]$
will match your criteria in a single line of code ...
 
Share this answer
 
Comments
Member 13067459 1-Jun-22 1:54am    
please tell me the answer as per my code becase i want impliment like that
Member 13067459 1-Jun-22 1:57am    
main this is im valdating 4th postion it should in
(A,B,C,P,T)
Member 13067459 1-Jun-22 2:03am    
question is updated please check once
Hi,
First spilt pan numbers and pass the spilt values for each functions for verify it, Here all function must return false, otherwise PAN number is incorrect.
In fourth position i pass characters to check as "ABCFGHLJPT", bcz u mention 4 position must contain specific character.
Dim st As String = "BKLPA9891H"
     Dim strfirst As String = st.Substring(0, 5)
     Dim strmiddle As String = st.Substring(5, 4)
     Dim strlast As String = st.Substring(9, 1)
     Dim strpo4 As String = st.Substring(3, 1)

     Dim chkchr As Boolean = IsCharAlphabet(strfirst)
     Dim chkdigit As Boolean = IsNumber(strmiddle)
     Dim chklastchr As Boolean = IsCharAlphabet(strlast)
     Dim chkIsABCFGHLJPT As Boolean = IsABCFGHLJPT(strpo4)

     If chkchr = True Or chkdigit = True Or chklastchr = True Or chkIsABCFGHLJPT = True Then
         Dim strError = "PAN numbers need to have 5 characters followed by 4 numbers then a final character. Please check your entry and try again"
     Else
         Dim strSuccess = "PAN number accpected"
     End If


Function to check spilt strings.
  Function IsNumber(s As String) As Boolean
        Dim strrtn As Boolean = False

        For i = 0 To s.Length - 1
            If Not System.Text.RegularExpressions.Regex.IsMatch(s(i), "^[0-9 ]+$") Then
                strrtn = True
            End If
        Next


        Return strrtn
    End Function

    Function IsCharAlphabet(s As String) As Boolean
        Dim strrtn As Boolean = False

        For i = 0 To s.Length - 1
            If Not System.Text.RegularExpressions.Regex.IsMatch(s(i), "^[A-Z]+$") Then
                strrtn = True
            End If
        Next


        Return strrtn
    End Function

    Function IsABCFGHLJPT(s As String) As Boolean
        Dim strrtn As Boolean = False
        Dim chkChar As String = "ABCFGHLJPT"
        For i = 0 To chkChar.Length - 1
            If Not s.Contains(chkChar(i)) Then
                strrtn = True
            Else
                strrtn = False
                GoTo Reachhere
            End If
        Next
Reachhere:
        Return strrtn
    End Function




From your code, i updated case 5, bcz from 1 to 5 is alphabet, but in 5 position must be some specific alphabet, so it check passing char is alphabet and it must any one of this character "ABCFGHLJPT". So no need if codition.
Select Case Pos
               Case 1 To 4
                   Test = TestForAlpha(Mid(PAN, Pos, 1))
               Case 5
                   Test = TestForAlpha4(Mid(PAN, Pos, 1))
               Case 6 To 9
                   Test = TestForNumeric(Mid(PAN, Pos, 1))
               Case 10
                   Test = TestForAlpha(Mid(PAN, Pos, 1))
           End Select





Thanks and Regards
Aravind.

Note: Pls accept answer is it useful to you. Happy Coding...
 
Share this answer
 
v4
Comments
Member 13067459 1-Jun-22 5:02am    
Good answer but i want answer as per my code and i want only specifically for 4th position
Aravindba 1-Jun-22 5:32am    
solution2 updated using ur code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900