0

I am trying to validate the input of a TextBox to make sure it is a Binary Number. What I have so far is:

Private Sub Command1_Click()
 Dim b() As Byte
 b = Text1.Text

 If Not IsByte (b) Then
    Text3 = "Wrong input"
    Else
    Text3 = "CRC is generated"
  '  checksum.Text = Text1.Text Xor Text2.Text
   ' Trans(2).Text = (Text1.Text) + (checksum.Text)
 End If

Input in Text1 should only be accepting binary numbers, so only 1 or 0 should be allowed.

3 Answers 3

4

You can use Like here:

Private Sub Command1_Click()
    If Len(Text1.Text) = 0 Or Text1.Text Like "*[!0-1]*" Then
        MsgBox "bad binary string"
    Else
        MsgBox "good binary string"
    End If
End Sub

This pattern is testing for "0 to many of anything, followed by one character not in the range 0 through 1, then 0 to many of anything."

Sign up to request clarification or add additional context in comments.

Comments

0

Below is the code. Request you to kindly google before posting any questions to this forum.

'will only allow 0 and 1
Private Sub Text1_KeyPress(KeyAscii As Integer)
 Select Case KeyAscii
    Case vbKey0 To vbKey1
    Case Else
        KeyAscii = 0
    End Select
End Sub

' will validate if its numeric and you can further check for 0 or 1
Private Sub Command1_Click()
        If Not IsNumeric(Text1.Text) Then
            MsgBox "Please enter numbers only.", vbInformation
            'you may also consider erasing it
            Text1.Text = ""
        End If
    End Sub

5 Comments

@Ricky-2135 your mozilla crashed but still you managed to post this question to stack overflow.
@Ricky-2135 kindly accept this answer before your mozilla or chrome crashes.
I want to but then its said need 15 reputations to vote, so sorry i just register today
@Ricky-2135 no worries :-)
this would be the answer i would go with
0

Even i cant find the function to check binary data. But cant you just check like

   If textbox1.text = 1 or textbox1.text = 2

I think you can also do with instr function.

3 Comments

yeah thank you its works, then how to initialize Text1 As Long?
can you accept the answer which will increase your reputation as well?
Accepting answer doesnt require any reputation. Its only upvoting that needs reputation. Please accept answer which shows your trust in people here. Just click on the mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.