|
 |
|
|
|
11-16-2004, 08:19 AM
|
Formating a phone number
|
Posts: 27
Location: QC, Canada
|
I need a ASP/VB method to take all the diffent ways someone might enter a phone number.
(222) 222-222
222-222-2222
2222222222
222 222 2222
1 222 222 2222
12222222222
and convert it to (222) 222-2222
Any one have a good solution?
Thanks
Last edited by netcrawler; 11-16-2004 at 08:30 AM..
|
|
|
|
11-16-2004, 09:44 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
I havn't tested this, but it should work:
Code:
Function FormatPhoneNumber(strNumber)
'First, remove all formating
strNumber = replace(strNumber, ")", "")
strNumber = replace(strNumber, "(", "")
strNumber = replace(strNumber, " ", "")
strNumber = replace(strNumber, "-", "")
'remove leading 1 if any
if len(strNumber) = 11 and left(strNumber, 1) = "1" then
strNumber = Right(strNumber, 10)
elseif len(strNumber) <> 10 then
'number has to be at least 10 digits
return "Invalid Number"
end if
'reformat the number
strNumber = "(" & Left(strNumber, 3)& ") " & Mid(strNumber, 4, 3) & "-" & Right(strNumber, 4)
return strNumber
End Function
|
|
|
|
11-16-2004, 10:05 AM
|
|
Posts: 27
Location: QC, Canada
|
Thanks and I forgot to add in my quote how will i output it to a web page?
response.write ??
the fuction is ok but I dont seem to have the output working it shows up with nothing and my database hase 1000's of numbers in it. Could you show me a example on how to go about to write the output to a webpage.
Thanks a bunch.
I have my function between the head, and i have this as my write:
response.write FormatPhoneNumber(rstSearch("unittelephone"))
and this is the fuction:
<%
Function FormatPhoneNumber(strNumber)
'First, remove all formating
strNumber = replace(strNumber, ")", "")
strNumber = replace(strNumber, "(", "")
strNumber = replace(strNumber, " ", "")
strNumber = replace(strNumber, "-", "")
'remove leading 1 if any
if len(strNumber) = 11 and left(strNumber, 1) = "1" then
strNumber = Right(strNumber, 10)
else
'
'elseif len(strNumber) <> 10 then
'number has to be at least 10 digits
'return "Invalid Number"
end if
'reformat the number
strNumber = "(" & Left(strNumber, 3)& ") " & Mid(strNumber, 4, 3) & "-" & Right(strNumber, 4)
End Function %>
Any help would be helpull thanks..
|
|
|
|
11-16-2004, 10:17 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
You seem to have modified the function and removed the last line...
return StrNumber
You need to put that back in in order for it to work.
|
|
|
|
11-16-2004, 10:26 AM
|
|
Posts: 27
Location: QC, Canada
|
This is the error I get if i keep the last line:
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'return'
/directory-repertoire/postal_search_e.asp, line 34
and if take it out i get empty fields..
??
Thanks.
|
|
|
|
11-16-2004, 10:41 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
Please change
TO
Code:
FormatPhoneNumber = strNumber
My apologies... this is what I get for programming in multiple languages 
|
|
|
|
11-16-2004, 10:49 AM
|
|
Posts: 1,626
Location: Guildford, UK
|
Quote:
|
this is what I get for programming in multiple languages
|
Yeah... I keep putting semi colons at the end of all my VB.NET.
(Ex-Delphi programmer...)
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"
Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
11-16-2004, 10:50 AM
|
|
Posts: 27
Location: QC, Canada
|
Nice its working were getting there .... I have another quick question if the value is equal to 0 meaning the field in the database is empty right now its showing this -- how can i make it show nothing?
Thanks alot for your help Anacrusis
|
|
|
|
11-16-2004, 10:54 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
netcrawler, you need to uncomment the lines you commented out and also fix my 'return' mistake:
Code:
elseif len(strNumber) <> 10 then
number has to be at least 10 digits
FormatPhoneNumber = ""
end if
|
|
|
|
11-16-2004, 11:02 AM
|
|
Posts: 27
Location: QC, Canada
|
That's odd it still shows up?
Unit trains Mondays at 1830 --
Unit trains Tuesdays at 1845 709-528-3071
Code:
Function FormatPhoneNumber(strNumber)
'First, remove all formating
strNumber = replace(strNumber, ")", "")
strNumber = replace(strNumber, "(", "")
strNumber = replace(strNumber, " ", "")
strNumber = replace(strNumber, "-", "")
'remove leading 1 if any
if len(strNumber) = 11 and left(strNumber, 1) = "1" then
strNumber = Right(strNumber, 10)
elseif len(strNumber) <> 10 then
'number has to be at least 10 digits
FormatPhoneNumber = ""
end if
'reformat the number
strNumber = Left(strNumber, 3)& "-" & Mid(strNumber, 4, 3) & "-" & Right(strNumber, 4)
FormatPhoneNumber = strNumber
End Function
end of code
and this is my response.write:
response.write FormatPhoneNumber(rstSearch("unittelephone"))
I dont understand?
|
|
|
|
11-16-2004, 11:29 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
change
Code:
'number has to be at least 10 digits
FormatPhoneNumber = ""
to
Code:
'number has to be at least 10 digits
FormatPhoneNumber = ""
Exit Function
|
|
|
|
11-16-2004, 11:44 AM
|
|
Posts: 27
Location: QC, Canada
|
You rock man!!! I wish one day to be a programmer like you!! I'm a neewbie and i love it so far I admit its alot of headaches but the end result is rewarding. If i need any more help along this script you don't mind if i ask you more questions?
Again thanks alot.
|
|
|
|
11-16-2004, 11:48 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
I'm glad I can help you.
You can ask questions anytime, that's what this forum is for. I'm not the only one here that can help you... there's alot of knowledge on these boards.
|
|
|
|
11-16-2004, 12:05 PM
|
|
Posts: 27
Location: QC, Canada
|
Thanks...
Just so that i can understand more how that script works would you mind explainig to me the function script what all the lines does if it's not too demanding I would like to understand what exaclty i just copied and pasted.. so that in the long run i could create my own fuctions.
Thanks.. alot.
|
|
|
|
11-16-2004, 12:20 PM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
Here is the function with each line commented, if you don't understand what a certain line of code is doing, let me know.
Code:
Function FormatPhoneNumber(strNumber)
'First, remove all existing formating
'We want the number to end up looking like this: 1234567890
strNumber = replace(strNumber, ")", "")
strNumber = replace(strNumber, "(", "")
strNumber = replace(strNumber, " ", "")
strNumber = replace(strNumber, "-", "")
'if there is 11 digits and the first digit is 1 then
if len(strNumber) = 11 and left(strNumber, 1) = "1" then
'grab the last 10 characters
strNumber = Right(strNumber, 10)
elseif len(strNumber) <> 10 then 'if the number is not 10 digits long, then it must be invalid
'Set the function return value to blank
FormatPhoneNumber = ""
'invalid number - let's exit this function, we're done.
exit function
end if
'reformat the number
'the left, mid, and right functions are used to get the peices of the number we need
'this is where we'll put the parentheses and dashes in the correct spot.
strNumber = "(" & Left(strNumber, 3) & ") " & Mid(strNumber, 4, 3) & "-" & Right(strNumber, 4)
'set the function return value to our newly formatted number
FormatPhoneNumber = strNumber
End Function
|
|
|
|
11-16-2004, 03:19 PM
|
|
Posts: 27
Location: QC, Canada
|
ok what should I do I found a number that was entered like this in the database:
111-111-1111x 1111
I need it to be: 111-111-1111 Ext: 1111
This is getting complicated im trying to understand the fuction explanation you gave me and its starting to sink in but i notice that phone # format and i kind of know what to do but its not working. What would you add to the code to have that type phone number formated corectly
Thanks,
|
|
|
|
11-18-2004, 11:12 AM
|
|
Posts: 27
Location: QC, Canada
|
Hi its me again could someone help me out with my question please.. I tried all kind of ways but its not working? I might be forgeting a small thing. Could someone give me a example script to fix my issue.
Thanks.
|
|
|
|
11-18-2004, 11:16 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
Hi, I apologize for not respondig to you sooner.
Do all phone numbers that have an extension start with x### ?
|
|
|
|
11-18-2004, 11:28 AM
|
|
Posts: 27
Location: QC, Canada
|
hi thanks for your reply.. since your asking about the formats of the ext: well here are all the formats of EXT: in the database.. tell you the truth the database is a mess in the phone number format.. here are the formats:
(111) 111-1111- 111
(111) 111-1111-111
(111)111-111ext1111
(111) 111-1111x1111
(111) 111 1111 -1111
(111) 111 1111 1111
(111) 111-1111 X 111
(111) 111-1111 ext 1
(111) 111-1111-111
(111) 111-1111(111)
(111) 111-1111(1111)
(111) 111-1111*1
(111)111-1111 EXT 11
(111)111-1111Ex1111
(111)111-111Ex1111
And the format required for the output is:
111-111-1111 Ext: whatever the number of characters the extention has.
ie: 111-111-1111 Ext:111 ---> only a example cause some extentions has 2-3-4-5 characters
Thanks.
Last edited by netcrawler; 11-18-2004 at 11:31 AM..
|
|
|
|
11-18-2004, 11:28 AM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
- edit -
I had posted a modified version of the function, but I just saw your last post with all the possiblities.... I'm going back to the drawing board 
|
|
|
|
|
« Reply to Formating a phone number
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|