Tazbirul Haque
Tazbirul Haque

Reputation: 233

Vb net Or logic is not working

I want to permit my user to save data if he has given 11 characters or 17 characters in the textbox. I have written this code for my vb net form input. But this logic is not working. if i remove the Or condition than this code works for 11 characters. But i want to implement for 11 and 17 characters both.

If (txtSSN.Text.Length <> 11 Or txtSSN.Text.Length <> 17) Then
    MessageBox.Show(" National ID should be 11 or 17 characters!!",
                    "Saving Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    txtSSN.Focus()
    Return False
End If

Upvotes: 0

Views: 124

Answers (1)

MatSnow
MatSnow

Reputation: 7527

These three examples will work:

If Not txtSSN.Text.Length = 11 And Not txtSSN.Text.Length = 17 Then
    MessageBox.Show("National ID should be 11 or 17 characters!!",
                    "Saving Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

If txtSSN.Text.Length <> 11 And txtSSN.Text.Length <> 17 Then
    MessageBox.Show("National ID should be 11 or 17 characters!!",
                    "Saving Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

If Not (txtSSN.Text.Length = 11 Or txtSSN.Text.Length = 17) Then
    MessageBox.Show("National ID should be 11 or 17 characters!!",
                    "Saving Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

Upvotes: 6

Related Questions