CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 1999
    Posts
    158

    How to validate a phone number

    I have this code for checking to see if a phone
    number is entered as numbers but it is not working
    could someone tell me why?

    If isempty(Phone) then
    PhoneIsOkay = false
    PageIsOkay = false

    else
    If len(Phone)<>8then
    PhoneIsOkay = false
    PageIsOkay = false

    i = 1
    While i < 8 'there are eight spaces that you want to check

    Select Case i
    Case 1,2,3 ' the positions in the string that should be numbers
    If Not IsNumeric(mid(Phone,i,1)) then 'Check the format

    PageIsOkay = false
    End If

    Case 4 'check that middle for a dash
    If IsNumberic(mid(Phone, i, 1)) then
    PageIsOkay =false

    End If

    Case 5,6,7 ' the positions in the string that should be numbers
    If Not IsNumeric(mid(Phone,i,1)) then 'Check the format

    PageIsOkay = false
    End If
    End Select
    i= i+1
    Wend
    End If
    End If




    Thanks
    Louise


  2. #2
    Join Date
    Jun 1999
    Posts
    158

    Re: How to validate a phone number

    resubmitting question


  3. #3
    Join Date
    May 1999
    Posts
    3,332

    Re: How to validate a phone number

    that's a lot of code for checking if a string consists of 3 digits, a dash and 3 digits, isn't it.
    try the regular expression object in VB6:

    Dim r as new RegExp
    r.Pattern = "\d{3}-\d{3}"
    MsgBox r.Test("513-1234")



    3 lines of code to do the same thing. r.Test returns true or false...
    add a reference to the microsoft vbscript regular expression object if you own ie5....


  4. #4
    Join Date
    Jan 2000
    Posts
    20

    Re: How to validate a phone number

    1. You have the number validating code embedded inside your "if len(Phone)<>8" clause...if the length of the string is actually 8, this is never executed.

    2. I'm not sure if you did this on purpose or not, but when you're checking for numeric characters and a requirement isn't met, you're only setting PageIsOkay to false and not PhoneIsOkay as well.

    3. Your while statement executes from characters 1 to 7 (i<8)...if I entered the eighth character as non-numeric, this wouldn't catch it.

    4. Checking for the dash, using IsNumeric as a test precludes the possibility that I might have entered a letter there.

    It might be better to use the Regular Expressions as was suggested. If not, another alternative with less code would be a for loop that looks like this.


    for i = 1 to 8
    If i<>4 then
    If Not IsNumeric(mid(phone, i, 1)) then
    phoneIsOkay = false
    PageIsOkay = false
    End If
    End If
    next i
    If mid(phone, 4, 1) <> "-" then
    PageIsOkay = false
    phoneIsOkay = false
    End If




    That should also give the same result with a little less code.

    xxMariusxx


  5. #5
    Join Date
    Jan 2000
    Posts
    2

    Re: How to validate a phone number

    what about :

    If Me.Phone Like "###-####" Then
    MsgBox "phoneok"
    Else
    MsgBox "phone not ok"
    Me.Phone.SetFocus
    End If

    ------------------------------------
    Working is the curse of the drinking class

  6. #6
    Join Date
    Jun 1999
    Posts
    158

    Re: How to validate a phone number

    Thanks for everybody's replies, the code seems much simplier then what I had, but I did figure out what I had wrong, I was missing an else before the while statement. And to your question about the i<8 when I finally got the checks to work I found out that I needed to change it to i<9 to get the last number.

    Thanks again

    Louise


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

Featured