Click here to Skip to main content
15,880,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want validate international phone number in vb6 without using regular expressions

only () + this charactersand numeric are allowed and it shold be below 20 digits.

What I have tried:

private sub txtPhoneNo_validate ( cancel As boolean)
if txtPhoneNo = "" then exit sub
if not Isnumeric(txtPhoneNo) then
msgbox "phone number shold be in numeric only"
Elseif len(txtPhoneNo) > 20
msgbox "phone number shold be in numeric only"
Exit sub
end if
end sub



I want to add internation format for validation in my code and () + this are the character only allowed
Posted
Updated 2-Jun-22 22:09pm

You need to examine each character in turn. If it starts with a plus sign, then the next digit(s) are the international country code. That may or may not be followed by a number in parenthesis which is the internal long distance indicator. That should be followed by numbers in a single group or multiple groups separated by spaces or hyphens.

A local phone number here in UK could be one of:
+44 779-345666
+44 (0)779345666
0779 345666
0208-3456667

etc.
 
Share this answer
 
Comments
Richard MacCutchan 3-Jun-22 5:21am    
Sorry, no> This forum is not here to do your work for you.
Richard MacCutchan 3-Jun-22 5:31am    
As I already explained, the baseline is to start examining each character in turn. If the first character is a plus sign then capture the next set of numbers until you find a non-numeric. That may be a space or an open parenthesis. At that point you can save the country code, and maybe even validate it. The next set may be a number in parenthesis, so check that.

BUT, you need to determine which other formats you may need to validate, so the first thing to do is collect information on all the different numbers you expect to process.
private sub txtPhone_check()

if not isnumeric(txtPhone) or len(txtPhone) > 20 then

 msgbox "Error in Phone Number"

end if

end sub


to avoid redundancy, see that var = "" is equal : len(var) = 0
you don't need to test it twice, isnumeric already proceed a test of type and consistancy.

look at VB constants ( vbNullstring , vbEmpty , vbTrue , vbFalse ... ) they are "fast access" because of existing in registers of .net and CLR. you'll gain execution time through those constants.
Vb have one hundred of constants for lot of use and components.


String replace will help you to insert the chars()+ you want.
 
Share this answer
 
v2
Comments
Dave Kreskowiak 3-Jun-22 8:14am    
they are "fast access" because of existing in registers of .net and CLR.

No, they are constants and there are no "registers" in ".NET and CLR". You do not get any performance boost by using constants. They are merely well-known names assigned specific values to make reading and writing code easier by eliminating "magic values".

Also, VB6 does not use .NET.
Member 15627495 3-Jun-22 8:47am    
Its VB 6 inheritance and VB .Net by microsoft tuition, why thinking when reading is enough ?

It's more than "syntax better". really.
Dave Kreskowiak 3-Jun-22 8:49am    
None of what you said made any sense at all.

They are just constants that are replaced by their actual values at compile time. That's all.

It has nothing to do with inheritance.
Member 15627495 3-Jun-22 9:06am    
you really don't know .Net ...
you're talking for yourself - stop with that.
Dave Kreskowiak 3-Jun-22 9:35am    
Oh yeah, using it for 20 years now and I don't know .NET. Sure, sparky.

Have you actually looked at your code in ILDASM? I doubt it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900