Click to See Complete Forum and Search --> : Validating a text box


manil
May 7th, 2008, 08:06 PM
hi! i want to make validation on a text box so that the first character is only A, B, C, or X..


sCustomerAccountNumber = txtCustomerAccountNumber.text

If sCustomerAccountNumber.Chars(0) <> Chr(65) Then
sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
bErrorOccurred = True
ElseIf sCustomerAccountNumber.Chars(0) <> Chr(66) Then
sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
bErrorOccurred = True
ElseIf sCustomerAccountNumber.Chars(0) <> Chr(67) Then
sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
bErrorOccurred = True
ElseIf sCustomerAccountNumber.Chars(0) <> Chr(88) Then
sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
bErrorOccurred = True
End If


what happens with these code is whenever i try to put in A, B, C, or X.. i get the error message.. should i convert this into a loop..??

manil
May 7th, 2008, 09:51 PM
can someone close this..?? already found out how to do it..

dglienna
May 7th, 2008, 09:55 PM
Click on Thread Tools, and then Mark as RESOLVED. Only you can close it out.

Also, it'd help the next person to post the answer to any question.

Thanks

Shuja Ali
May 8th, 2008, 02:34 AM
You should also look at Regular Expressions. You will have to write less code and it will be easier to maintain and understand.

manil
May 20th, 2008, 07:54 PM
what's Regular Expressions..??

this is what i did:


sChar1 = Mid(txtCustomerAccountNumber.text, 1, 1)

If sChar1 <> "A" And sChar1 <> "B" And sChar1 <> "C" And sChar1 <> "X" Then
sErrorMessage = sErrorMessage & "Customer Acount Number should start with A, B, C, or X" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
bErrorOccurred = True


but it didn't work properly... any suggestions..??

Alsvha
May 20th, 2008, 10:39 PM
Use a regular expression such as Shuja Ali suggests to make the validation logic.

You can take a look at it here:
http://msdn.microsoft.com/en-us/library/hs600312.aspx

It is a powerful tool and well worth the time to get introduced to.

WillAtwell
May 21st, 2008, 02:43 AM
Normally I just use an instr statement for such checks.


Dim ValidChars as String="ABCX"

if instr(ValidChars,Mid(txtCustomerAccountNumber.text, 1, 1))=0 then

' do error stuff here
end if

Marraco
May 21st, 2008, 07:34 AM
hi! i want to make validation on a text box so that the first character is only A, B, C, or X..
... Select Case sCustomerAccountNumber.Chars(0)
Case "A", "B", "C", "X"
'Is ok
Case Else
'Is Wrong
sErrorMessage &= "Customer Account Number should start with a letter!" & vbCrLf & _
"e.g. A, B, C or X" & vbCrLf & _
"e.g. A1234567" & vbCrLf
bErrorOccurred = True
End Select

HairyMonkeyMan
May 21st, 2008, 07:39 AM
How about a custom user control that takes a regular expression as a property and an isvalid property that gets set when valid - you could include an error provider in the control.