user2196860
user2196860

Reputation: 3

Visual Basic minimum 5 characters validation

How would I go about making sure that when a user inputs his "Combination" into the input box, it has to be at least 5 characters?

This is what code I have so far:

If (tboxStatus.Text) = "Combination Not Set" Or (tboxStatus.Text) = "UnLocked" Then
  Combination = CInt(InputBox("Set the Combination First"))
  tboxStatus.Text = "Locked"
ElseIf (tboxStatus.Text) = "Locked" Then
  MsgBox("You must first UnLock the safe before trying to change the combination.")
End If

Upvotes: 0

Views: 2357

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 416059

If tboxStatus.Text = "Combination Not Set" OrElse tboxStatus.Text = "UnLocked" Then
    Dim result As String = ""

    While String.IsNullOrWhiteSpace(result) OrElse Not Integer.TryParse(result, Combination)
       result= InputBox("Set a Combination Number First")
    End While

    tboxStatus.Text = "Locked"

ElseIf tboxStatus.Text = "Locked" Then
    MsgBox("You must first UnLock the safe before trying to change the combination.")
End If

Upvotes: 0

mr_plum
mr_plum

Reputation: 2437

For starters...

Dim value as String = InputBox("Set the Combination First")
If (value.Trim.Length < 5) Then
    MsgBox ("Combination must be at least 5 characters")
Else
    Combination = CInt(value)
End If

Among other things, you'll need to check if it's numeric before doing a CInt()

Upvotes: 1

Related Questions