Phone number validation to exclude space (1 Viewer)

aman

Registered User.
Local time
Today, 01:41
Joined
Oct 16, 2008
Messages
1,250
Hi Guys

I have written the following piece of code that will check if the phone number is numeric or not . But I want to add another check in this so that if there is a space in the phone number then just ignore it . As normally there is a space after postal code so my current code doesn't count a phone number numeric if there is a space.

Can anyone please make changes in the below code accordingly?

Code:
Public Function NumbersOnly(ByVal strText As String) As Boolean
    Dim intCounter As Integer
    For intCounter = 1 To Len(strText)
        If Not IsNumeric(Mid(strText, intCounter, 1)) Then
            NumbersOnly = False
            Exit Function
        End If
    Next intCounter
    NumbersOnly = True
End Function

Thanks
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:41
Joined
Jan 20, 2009
Messages
12,849
Remove the spaces prior to the test with
Code:
Replace(strText, " ", "")
 

aman

Registered User.
Local time
Today, 01:41
Joined
Oct 16, 2008
Messages
1,250
Galaxiom , where do I need to make this change??
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:41
Joined
Jan 20, 2009
Messages
12,849
BTW Someone might suggest testing the whole string with IsNumeric().

It wouldn't be reliable because strings like "2E5" and "3N6" both return true with IsNumeric.
 

isladogs

MVP / VIP
Local time
Today, 08:41
Joined
Jan 14, 2017
Messages
18,186
Your function will fail if the string contains a space, starts with zero or + or (.
All of these are common in phone numbers

For example:
1754324562 => true
01754 324562 => false
+44 1754 324562 => false
(44) 1754 324562 => false
1754 324562 => false

This will fix all of those issues

Code:
Public Function NumbersOnly(ByVal strText As String) As Boolean

    Dim intCounter As Integer
    
    'remove leading zero
    If Left(strText, 1) = "0" Then strText = Mid(strText, 2)
    
    'remove unwanted characters
    strText = Replace(strText, " ", "")
    strText = Replace(strText, "+", "")
    strText = Replace(strText, "(", "")
    strText = Replace(strText, ")", "")
    
    'or combine the above 4 lines into 1 line
   ' strText = Replace(Replace(Replace(Replace(strText, " ", ""), "+", ""), "(", ""), ")", "")
    
    For intCounter = 1 To Len(strText)
        If Not IsNumeric(Mid(strText, intCounter, 1)) Then
            NumbersOnly = False
            Exit Function
        End If
    Next intCounter
    NumbersOnly = True
End Function

All the above values now are marked as true

EDIT Just saw Galaxiom's reply so tested these results:
1754N324562 => false
1754E324562 => false

So that's still OK
Suggest checking with some other real life examples in case something else breaks it
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:41
Joined
Jan 20, 2009
Messages
12,849
You could so this in the first line.

Code:
strText = Replace(strText, " ", "")

NB Ensure you don't leave out the ByVal or it will change the parameter through the call.

(ridders covered it better)
 

aman

Registered User.
Local time
Today, 01:41
Joined
Oct 16, 2008
Messages
1,250
Thats great Ridders. It works perfectly :)
 

isladogs

MVP / VIP
Local time
Today, 08:41
Joined
Jan 14, 2017
Messages
18,186
You could so this in the first line.

Code:
strText = Replace(strText, " ", "")

NB Ensure you don't leave out the ByVal or it will change the parameter through the call.

(ridders covered it better)

LOL.
Now that's praise indeed Greg!
I've waited a long time to hear that!!!!
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:41
Joined
Jan 20, 2009
Messages
12,849
EDIT Just saw Galaxiom's reply so tested these results:
1754N324562 => false
1754E324562 => false

N and E stand for exponent.

Any expression that exceeds the limit of a Double will return False.
That limit is approximately:
1.797693134862E308

Someone might enter a ten digit phone number with a three digit extension and it would return True:
1234567890E277

That is one reason why the characters need to be tested individually rather than the whole expression.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:41
Joined
Jan 20, 2009
Messages
12,849
LOL.
Now that's praise indeed Greg!
I've waited a long time to hear that!!!!

I tend to only point out things I don't agree with or think need clarification.
Take it as praise when I say nothing.;)

If it helps, it was me that suggested you as a VIP due to your prolific activity and consistently sound advice.
 

isladogs

MVP / VIP
Local time
Today, 08:41
Joined
Jan 14, 2017
Messages
18,186
N and E stand for exponent.
Yes I realise what E stands for. Not seen N before

Any expression that exceeds the limit of a Double will return False.
That limit is approximately:
1.797693134862E308

Someone might enter a ten digit phone number with a three digit extension and it would return True:
1234567890E277

That is one reason why the characters need to be tested individually rather than the whole expression.

That's why I wrote:
ridders said:
Suggest checking with some other real life examples in case something else breaks it

EDIT: Just saw this....
Galaxiom said:
I tend to only point out things I don't agree with or think need clarification.
Take it as praise when I say nothing.

If it helps, it was me that suggested you as a VIP due to your prolific activity and consistently sound advice.

Aw shucks. Thank you! :eek:
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:41
Joined
Jan 23, 2006
Messages
15,361
aman
I'm not sure what range of phone numbers you will validate, but I have seen the "-" used quite often. I did Google search and found this (it's older, but it's the info that may be relevant). So the "-" , "." and "/" may be applicable additions.

I agree with comments by Colin and Galaxiom.
Good luck with your project.
 

isladogs

MVP / VIP
Local time
Today, 08:41
Joined
Jan 14, 2017
Messages
18,186
aman
I'm not sure what range of phone numbers you will validate, but I have seen the "-" used quite often. I did Google search and found this (it's older, but it's the info that may be relevant). So the "-" , "." and "/" may be applicable additions.

I agree with comments by Colin and Galaxiom.
Good luck with your project.

Forgot about "-" so have added it below

Code:
Public Function NumbersOnly(ByVal strText As String) As Boolean

    'use to check phone 'numbers' are numeric

    Dim intCounter As Integer
    
    'remove leading zero
    If Left(strText, 1) = "0" Then strText = Mid(strText, 2)
    
    'remove unwanted characters
    strText = Replace(strText, " ", "")
    strText = Replace(strText, "+", "")
    strText = Replace(strText, "(", "")
    strText = Replace(strText, ")", "")
    strText = Replace(strText, "-", "")
    
    'or combine the above 5 lines into 1 line
    'strText = Replace(Replace(Replace(Replace(Replace(strText, " ", ""), "+", ""), "(", ""), ")", ""), "-", "")
    
    For intCounter = 1 To Len(strText)
        If Not IsNumeric(Mid(strText, intCounter, 1)) Then
            NumbersOnly = False
            Exit Function
        End If
    Next intCounter
    NumbersOnly = True
End Function

Haven't included "." or "/" as I've never seen them in phone numbers.
Someone else may be aware of those however.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:41
Joined
May 7, 2009
Messages
19,169
So that is why i never become a VIP. i remember it was 2 years ago when i get the "vip" but only on the forum's greetings. It never appeared on my avatar. Then minty became one and mr.ridders. they have it on their avatar. But no problem, just remove "Newly" in my avatar coz im not a newly registered user.
 

isladogs

MVP / VIP
Local time
Today, 08:41
Joined
Jan 14, 2017
Messages
18,186
Just change it yourself in your profile which is what we did after getting the invite!
 

Users who are viewing this thread

Top Bottom