Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
Formating a phone number
Old 11-16-2004, 08:19 AM Formating a phone number
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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..
netcrawler is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-16-2004, 09:44 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
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
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 10:05 AM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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..
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 10:17 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
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.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 10:26 AM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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.
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 10:41 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
Please change
Code:
return strNumber
TO
Code:
FormatPhoneNumber = strNumber

My apologies... this is what I get for programming in multiple languages
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 10:49 AM
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
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
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 11-16-2004, 10:50 AM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 10:54 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
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
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 11:02 AM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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?
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 11:29 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
change
Code:
'number has to be at least 10 digits
FormatPhoneNumber = ""
to
Code:
'number has to be at least 10 digits
FormatPhoneNumber = ""
Exit Function
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 11:44 AM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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.
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 11:48 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
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.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 12:05 PM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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.
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 12:20 PM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
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
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-16-2004, 03:19 PM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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,
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-18-2004, 11:12 AM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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.
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-18-2004, 11:16 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
Hi, I apologize for not respondig to you sooner.

Do all phone numbers that have an extension start with x### ?
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 11-18-2004, 11:28 AM
netcrawler's Avatar
Average Talker

Posts: 27
Location: QC, Canada
Trades: 0
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..
netcrawler is offline
Reply With Quote
View Public Profile
 
Old 11-18-2004, 11:28 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
- 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
Anacrusis is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Formating a phone number

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53096 seconds with 12 queries