Results 1 to 10 of 21

Thread: Validating PAN (Indian Format)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    There is always a VB solution, a UDF (user defined function) in this case...

    Code:
    Function IsPAN(S As String) As Boolean
      IsPAN = S Like "[A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z]####[A-Za-z]"
    End Function
    For those reading this who are unfamiliar with UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use IsPAN just like it was a built-in Excel function. For example,

    =IsPAN(A1)
    Hi

    There is one more requirement in this UDF, 4Th Character should be P for Individulas, C for Company & so on.

    So how can we check that & also whether we can give output with error instead of say True or False.

    EG. Length should be 10, First 5 character should be text, Next 6-9 character should be number, Last character should be text, 4th character should be either of P,H,F,A,T,B,L,J,G.

    Thanks & Regards

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by in.vaibhav View Post
    Hi

    There is one more requirement in this UDF, 4Th Character should be P for Individulas, C for Company & so on.

    So how can we check that & also whether we can give output with error instead of say True or False.

    EG. Length should be 10, First 5 character should be text, Next 6-9 character should be number, Last character should be text, 4th character should be either of P,H,F,A,T,B,L,J,G.

    Thanks & Regards
    This should account for the requirement you posted about...
    Code:
    Function IsPAN(S As String) As Boolean
      IsPAN = S Like "[A-Za-z][A-Za-z][A-Za-z][ABFGHJLPTabfghjlpt][A-Za-z]####[A-Za-z]"
    End Function

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0
    Quote Originally Posted by Rick Rothstein View Post
    This should account for the requirement you posted about...
    Code:
    Function IsPAN(S As String) As Boolean
      IsPAN = S Like "[A-Za-z][A-Za-z][A-Za-z][ABFGHJLPTabfghjlpt][A-Za-z]####[A-Za-z]"
    End Function
    Hi

    I wanted to function like this..
    Code:
    Function ISPAN(inData As String)
    Dim i As Integer
    Dim Result As String
    
    If Len(inData) <> 10 Then
    ISPAN = "Total Character is " & Len(inData)
    Exit Function
    End If
    
    If Mid(UCase(inData), 1, 1) Like "[A-Z]" Then
    r1 = ""
    Else
    r1 = "Char1 is not Text,"
    End If
    
    If Mid(UCase(inData), 2, 1) Like "[A-Z]" Then
    r2 = ""
    Else
    r2 = "Char2 is not Text,"
    End If
    
    If Mid(UCase(inData), 3, 1) Like "[A-Z]" Then
    r3 = ""
    Else
    r3 = "Char3 is not Text,"
    End If
    
    If Mid(UCase(inData), 4, 1) Like "[P]" Then
    r4 = ""
    Else
    r4 = "Char4 Should be P"
    End If
    
    If Mid(UCase(inData), 5, 1) Like "[A-Z]" Then
    r5 = ""
    Else
    r5 = "Char5 is not Text,"
    End If
    
    If Mid(UCase(inData), 6, 1) Like "#" Then
    r6 = ""
    Else
    r6 = "Char6 is not Number,"
    End If
    
    If Mid(UCase(inData), 7, 1) Like "#" Then
    r7 = ""
    Else
    r7 = "Char7 is not Number,"
    End If
    
    If Mid(UCase(inData), 8, 1) Like "#" Then
    r8 = ""
    Else
    r8 = "Char8 is not Number,"
    End If
    
    If Mid(UCase(inData), 9, 1) Like "#" Then
    r9 = ""
    Else
    r9 = "Char9 is not Number,"
    End If
    
    If Mid(UCase(inData), 10, 1) Like "[A-Z]" Then
    r10 = ""
    Else
    r10 = "Char10 is not Text,"
    End If
    
    Result = r1 & r2 & r3 & r4 & r5 & r6 & r7 & r8 & r9 & r10
    
    If Right(Result, 1) = "," Then
        Result = Left(Result, Len(Result) - 1)
    End If
    
    If Result = "" Then
        ISPAN = "OK"
            Else
        ISPAN = Result
        End If
    End Function
    This works fine but it is very lengthy can we shortn same by looping, I am not expert in VBA

    Thanks for all support.


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    I don't know this is a lot shorter than you were hoping for, but given you wanted complete error reporting, looping does not really help that much. I noticed you only tested the 4th character to be the letter "P" in your code and, since you said your code works fine, I restricted the check of the 4th character to only that letter. Note that I also added a check to see if the argument is too short or too long to be a proper entry. Here is the non-looping code I came up with...
    Code:
    Function IsPAN(S As String) As String
      If Len(S) < 10 Then
        IsPAN = "Too short!"
      ElseIf Len(S) > 10 Then
        IsPAN = "Too long!"
      Else
        If Mid(S, 1, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 1 is not text."
        If Mid(S, 2, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 2 is not text."
        If Mid(S, 3, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 3 is not text."
        If Mid(S, 4, 1) Like "[!Pp]" Then IsPAN = IsPAN & vbLf & "Character 4 is not the letter 'P'."
        If Mid(S, 5, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 5 is not text."
        If Mid(S, 6, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 6 is not a number."
        If Mid(S, 7, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 7 is not a number."
        If Mid(S, 8, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 8 is not a number."
        If Mid(S, 9, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 9 is not a number."
        If Mid(S, 10, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 10 is not text."
        If Len(IsPAN) Then
          IsPAN = Mid(IsPAN, 2)
        Else
          IsPAN = "OK"
        End If
      End If
    End Function
    In case you are interested, here is the same function using loops (among other things)...
    Code:
    Function IsPAN(S As String) As String
      Dim X As Long, PatternChar As String, Test As New Collection
      Const Pattern As String = "AAAPANNNNA"
      If Len(S) < 10 Then
        IsPAN = "Too short!"
      ElseIf Len(S) > 10 Then
        IsPAN = "Too long!"
      ElseIf Len(IsPAN) = 0 Then
        Test.Add "[A-Za-z]", "A"
        Test.Add "[Pp]", "P"
        Test.Add "#", "N"
        For X = 1 To Len(S)
          PatternChar = Test(Mid(Pattern, X, 1))
          If Not Mid(S, X, 1) Like PatternChar Then
            IsPAN = IsPAN & vbLf & "Character " & X & " is not " & Choose(InStr("APN", _
                    Mid(Pattern, X, 1)), "text.", "the letter ""P"".", "a number.")
          End If
        Next
        If Len(IsPAN) Then
          IsPAN = Mid(IsPAN, 2)
        Else
          IsPAN = "OK"
        End If
      End If
    End Function

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    0


    Thanks. Rilli good piece of work..:cool :

    Once again thanks a lot!!!


    Quote Originally Posted by Rick Rothstein View Post
    I don't know this is a lot shorter than you were hoping for, but given you wanted complete error reporting, looping does not really help that much. I noticed you only tested the 4th character to be the letter "P" in your code and, since you said your code works fine, I restricted the check of the 4th character to only that letter. Note that I also added a check to see if the argument is too short or too long to be a proper entry. Here is the non-looping code I came up with...
    Code:
    Function IsPAN(S As String) As String
      If Len(S) < 10 Then
        IsPAN = "Too short!"
      ElseIf Len(S) > 10 Then
        IsPAN = "Too long!"
      Else
        If Mid(S, 1, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 1 is not text."
        If Mid(S, 2, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 2 is not text."
        If Mid(S, 3, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 3 is not text."
        If Mid(S, 4, 1) Like "[!Pp]" Then IsPAN = IsPAN & vbLf & "Character 4 is not the letter 'P'."
        If Mid(S, 5, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 5 is not text."
        If Mid(S, 6, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 6 is not a number."
        If Mid(S, 7, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 7 is not a number."
        If Mid(S, 8, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 8 is not a number."
        If Mid(S, 9, 1) Like "[!0-9]" Then IsPAN = IsPAN & vbLf & "Character 9 is not a number."
        If Mid(S, 10, 1) Like "[!A-Za-z]" Then IsPAN = IsPAN & vbLf & "Character 10 is not text."
        If Len(IsPAN) Then
          IsPAN = Mid(IsPAN, 2)
        Else
          IsPAN = "OK"
        End If
      End If
    End Function
    In case you are interested, here is the same function using loops (among other things)...
    Code:
    Function IsPAN(S As String) As String
      Dim X As Long, PatternChar As String, Test As New Collection
      Const Pattern As String = "AAAPANNNNA"
      If Len(S) < 10 Then
        IsPAN = "Too short!"
      ElseIf Len(S) > 10 Then
        IsPAN = "Too long!"
      ElseIf Len(IsPAN) = 0 Then
        Test.Add "[A-Za-z]", "A"
        Test.Add "[Pp]", "P"
        Test.Add "#", "N"
        For X = 1 To Len(S)
          PatternChar = Test(Mid(Pattern, X, 1))
          If Not Mid(S, X, 1) Like PatternChar Then
            IsPAN = IsPAN & vbLf & "Character " & X & " is not " & Choose(InStr("APN", _
                    Mid(Pattern, X, 1)), "text.", "the letter ""P"".", "a number.")
          End If
        Next
        If Len(IsPAN) Then
          IsPAN = Mid(IsPAN, 2)
        Else
          IsPAN = "OK"
        End If
      End If
    End Function

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Rep Power
    0
    Hello Sir,

    The above formula works fine.

    Thanks for the same. Would request you to let us know how to save the same in excel so that every time no need of pasting the above formula again ana again.

    Regards,
    Uday

  7. #7
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by udaysdevadiga@gmail.com View Post
    The above formula works fine.

    Thanks for the same. Would request you to let us know how to save the same in excel so that every time no need of pasting the above formula again ana again.
    Do you really mean "formula" or are you referring to the VB code? There are 15 prior messages in this thread, some with formulas and some with VB code solutions... what is the number for the message containing the "formula" you are referring to (look for the number on the right side of the title bar for the message)?

Similar Threads

  1. Validating PAN (Indian Format)
    By Admin in forum Test Area
    Replies: 30
    Last Post: 03-22-2023, 06:14 PM
  2. Validating PAN (Indian Format)
    By Admin in forum Test Area
    Replies: 20
    Last Post: 03-22-2023, 06:14 PM
  3. Validating PAN NUMBER Indian Format
    By mani780 in forum Excel Help
    Replies: 3
    Last Post: 03-10-2015, 01:19 PM
  4. Excel Number Format: Indian Style Comma Separation
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 6
    Last Post: 09-18-2013, 11:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •