Results 1 to 9 of 9

Thread: Phone number validate

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2001
    Location
    Gold Coast, Australia
    Posts
    7

    Exclamation 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.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary, Canada
    Posts
    453
    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!"

  3. #3
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary, Canada
    Posts
    453
    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!"

  4. #4
    Addicted Member bbosh's Avatar
    Join Date
    Oct 2000
    Location
    Hell (AKA New Mexico)
    Posts
    186
    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!

  5. #5
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary, Canada
    Posts
    453
    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!"

  6. #6
    Addicted Member bbosh's Avatar
    Join Date
    Oct 2000
    Location
    Hell (AKA New Mexico)
    Posts
    186
    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!

  7. #7
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary, Canada
    Posts
    453
    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!"

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2001
    Location
    Gold Coast, Australia
    Posts
    7
    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.

  9. #9
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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
  •  



Click Here to Expand Forum to Full Width