Code to check first and last letter.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ggftw
    New Member
    • Feb 2013
    • 3

    Code to check first and last letter.

    Hi,
    I have a field named CustomerNRIC and have an input mask of L0000000L. I need a code ( at OnEnter I assume ) so that when I enter the IC, the first letter must be either "S" or "T" and the last letter must be either from "A" - "J" or the letter "Z". If the user fails to do so, I will need a message box that says "Invalid ID".
    Thanks.
    Last edited by ggftw; Feb 21 '13, 10:26 AM. Reason: Thanks for the responses, really appreciated!
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    I'm not sure if validation rules can do this, but you might look into Validation Rules.

    Another way would be to use the control's BeforeUpdate event to check the value of the control using the Left() and Right() functions like this (This assumes that the control name on your form is CustomerNRIC):
    Code:
    Dim strMsg as String
    Dim intFlag as Integer
    
    intFlag = 0
    
    If Left(Me.CustomerNRIC, 1) <> "S" or Left(Me.CustomerNRIC,1) <> "T" Then
    	strMsg = "The first letter must be either S or T"
    	intFlag = 1
    End If
    
    If Right(Me.CustomerNRIC, 1) <> "A" or Right(Me.CustomerNRIC, 1) <> "J" Then
    	intFlag = 1
    	If strMsg & "" <> "" Then
    		strMsg = strMsg & VbCrLf & "and "
    	End If
    	strMsg = strMsg & "The last letter must be either A or J."
    End If
    
    If intFlag = 1 Then
    	MsgBox strMsg
    	Cancel = True
    End If

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      I would handle this in the BeforeUpdate event of the control:

      Code:
      Private Sub CustomerNRIC_BeforeUpdate(Cancel as Integer)
        If nz(Me.CustomerNRIC,"")="" then 
          'Field has been cleared. Dunno whether you allow this
          Cancel=True
          Exit sub
        End if
      
        If left(Me.CustomerNRIC,1)<>"S" And left(Me.CustomerNRIC,1)<>"T" Then
          Msgbox "Customer NRIC must start with S or T"
          Cancel=true
          Exit Sub
        End if
      
        Select Case Right(Me.CustomerNRIC,1)
          Case "A","B","C","D","E","F","G","H","I","J","Z"
            'All good
          Case Else
            Msgbox "Customer NRIC must end with A-J or Z"
            Cancel=True
            Exit Sub
        End If
      
      
      End Sub

      Comment

      • gvuksa
        New Member
        • Feb 2013
        • 7

        #4
        Simply extract first and last letter and check condition.
        Here is C# code:
        Code:
        private void btnEnter_Click(object sender, EventArgs e)
                {
                    //Get ID to string
                    string s = textBoxID.Text.ToUpper();
        
                    //Get IDs first letter
                    string firstLetter = s.Remove(1, s.Length - 1).ToUpper();
        
                    //Check last letter
                    bool lastLetter = CheckLastLetter(s.Remove(0, s.Length - 1));
        
                    //Check if ID is correct and show error message
                    if (firstLetter.Equals("T") || firstLetter.Equals("S"))
                    {
                        if (!lastLetter)
                            MessageBox.Show("Incorrect ID\nLast letter is incorrect");
                        else
                        {
                            //Login
                            MessageBox.Show("Jeeeee");
                        }
                    }
                    else
                        MessageBox.Show("Incorrect ID\nFirst letter must be \"T\" or \"S\"");
                    
                }
        
                private bool CheckLastLetter(string lastLetter)
                {
                    string[] lastLetters = new string[10] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
                    foreach (string s in lastLetters)
                    {
                        //Compare last letter with required letter
                        if (s == lastLetter)
                            return true; //Returns true if condition is satisfied
                    }
                    return false;            
                }

        Comment

        • Seth Schrock
          Recognized Expert Specialist
          • Dec 2010
          • 2965

          #5
          Oops, I missed that the last letter was A through J or Z. I just saw A or J.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            @gvuksa, this is the VBA forum, not the C# forum.

            Comment

            Working...