|
-
May 7th, 2008, 08:06 PM
#1
Validating a text box
hi! i want to make validation on a text box so that the first character is only A, B, C, or X..
Code:
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..??
-
May 7th, 2008, 09:51 PM
#2
Re: Validating a text box
can someone close this..?? already found out how to do it..
-
May 7th, 2008, 09:55 PM
#3
Re: Validating a text box
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
-
May 8th, 2008, 02:34 AM
#4
Re: Validating a text box
You should also look at Regular Expressions. You will have to write less code and it will be easier to maintain and understand.
-
May 20th, 2008, 07:54 PM
#5
Re: Validating a text box
what's Regular Expressions..??
this is what i did:
Code:
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..??
-
May 20th, 2008, 10:39 PM
#6
Re: Validating a text box
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.
-
May 21st, 2008, 02:43 AM
#7
Re: Validating a text box
Normally I just use an instr statement for such checks.
Code:
Dim ValidChars as String="ABCX"
if instr(ValidChars,Mid(txtCustomerAccountNumber.text, 1, 1))=0 then
' do error stuff here
end if
Last edited by WillAtwell; May 21st, 2008 at 02:45 AM.
-
May 21st, 2008, 07:34 AM
#8
Re: Validating a text box
 Originally Posted by manil
hi! i want to make validation on a text box so that the first character is only A, B, C, or X..
...
Code:
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
-
May 21st, 2008, 07:39 AM
#9
Re: Validating a text box
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|