|
-
Jun 10th, 2001, 12:09 AM
#1
Thread Starter
New Member
HELP! Stuck on phone number validate
Hello people,
I am in the process of writing my major Yr 12 visual basic case study, and have run into a little problem. I am trying to check that when the txtPhone_Num field loses focus a sub will check 1 digit at a time to make sure that only numbers have been entered.
Anyway this is what i've got so far:
Private Sub txtPhone_Num_LostFocus()
If txtPhone_Num.Text <> "" Then
Phone_NumValidate
End If
End Sub
Private Sub Phone_NumValidate()
Dim i As Integer, char As String * 1, sentence As String
sentence = txtPhone_Num
i = 1
For i = 1 To Len(sentence)
char = Mid(sentence, i, 1)
If char <> "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "8" Or "9" Or "0" Then
intpress = MsgBox("Please enter a valid phone number containing numbers only", vbExclamation + vbOKOnly, "Error!")
txtPhone_Num = ""
txtPhone_Num.SetFocus
Exit Sub
End If
i = i + 1
Next i
End Sub
Anyway the problem is that the error comes up every time, even when i do just put numbers in. Can anyone their help?
Thanks.[PHP]
Last edited by sponger58; Jun 10th, 2001 at 12:53 AM.
-
Jun 10th, 2001, 01:13 AM
#2
Hyperactive Member
Okay the problem is with the if statement.
You can't say if char <> "1" or "2" etc..
it has to be
if char <>"1" or char <>"2" or char <>"3".....
as you can see this would get very boring, but you can do
Code:
Private Sub txtPhone_Num_LostFocus()
If txtPhone_Num.Text <> "" Then
Phone_NumValidate
End If
End Sub
Private Sub Phone_NumValidate()
If trim$(txtPhone_Num.text) = cstr(val(txtPhone_Num.text)) Then
MsgBox"Please enter a valid phone number containing numbers only", vbExclamation + vbOKOnly, "Error!"
txtPhone_Num = ""
txtPhone_Num.SetFocus
End If
End Sub
This should work for all phone numbers except those starting with 0 (are there any in your app?).
I hope this helps,
SD
"I'd rather have a full bottle in front of me than a full frontal lobotomy!"
-
Jun 10th, 2001, 01:15 AM
#3
Hyperactive Member
Also, instead of erasing the phone number it might be more user friendly that, if they get it wrong then just highlight the text (using .selstart and .sellength). This way they can see what they've typed in, rather than having to retype the whole lot.
SD
"I'd rather have a full bottle in front of me than a full frontal lobotomy!"
-
Jun 10th, 2001, 01:26 AM
#4
Addicted Member
What ever you do, don't change the Or's to And's.
I have no idea what surf dameon's talking about in his next post
Last edited by bbosh; Jun 10th, 2001 at 01:37 AM.
Brian
Programming: VB5 Pro (SP3) - QBasic 1.1,4.5
Internet: HTML 4, CSS, JavaScript
Visit AltInt.com!
-
Jun 10th, 2001, 01:30 AM
#5
Hyperactive Member
Er, if you change the or's to and's it would never work. 
SD
"I'd rather have a full bottle in front of me than a full frontal lobotomy!"
-
Jun 10th, 2001, 01:35 AM
#6
Addicted Member
why not do validation as it's typed?
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 13 Or KeyAscii = 8 Then
Else
KeyAscii = 0
End If
End Sub
Brian
Programming: VB5 Pro (SP3) - QBasic 1.1,4.5
Internet: HTML 4, CSS, JavaScript
Visit AltInt.com!
-
Jun 10th, 2001, 01:38 AM
#7
Hyperactive Member
That is a good idea. I would also allow for spaces and dashes as people usually put them into phone numbers. You can always strip them out when you come to process the number.
SD
"I'd rather have a full bottle in front of me than a full frontal lobotomy!"
-
Jun 10th, 2001, 01:45 AM
#8
Thread Starter
New Member
Thanks heaps for that guys, i certainly wouldn't of thought of going about it that way. Oh well, i guess that comes with experience doesn't it. You know, just figuring out all the different ways of doing the one thing. THANKS.
Last edited by sponger58; Jun 10th, 2001 at 02:00 AM.
-
Jun 10th, 2001, 02:09 AM
#9
Registered User
This code prevents non numeric data from being entered.
Code:
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const ES_NUMBER = &H2000&
Const GWL_STYLE = (-16)
Private Sub Form_Load()
SetWindowLong Text1.hwnd, GWL_STYLE, GetWindowLong(Text1.hwnd, GWL_STYLE) Or ES_NUMBER
End Sub
Also check out the masked edit box, it might just be what you need
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
|