Click to See Complete Forum and Search --> : How to validate a phone number


LouLou
January 28th, 2000, 02:29 PM
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

LouLou
January 31st, 2000, 10:50 AM
resubmitting question

Lothar Haensler
January 31st, 2000, 11:02 AM
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....

xxMariusxx
January 31st, 2000, 01:05 PM
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

georgvs
January 31st, 2000, 01:30 PM
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

LouLou
February 1st, 2000, 09:11 AM
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