Solved

How to add (-) to a phone number ?

Posted on 2013-01-11
6
296 Views
Last Modified: 2013-01-13
This is my code, but do not working...do not know how to read though the whole array
If user enters 3034445555 the result should be 303-444-5555

Thanks for your help,

$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
Write-Host "$($InputTelephoneNumber)"
If (($InputTelephoneNumber[3] -ne "-") -and ($InputTelephoneNumber[3] -ne "-")) {
    $TempInputTelephoneNumber = $InputTelephoneNumber
	For ($i=11; $i –gt -1; $i--) {
	    For ($j=9; $j –gt -1; $j--) {
	          $InputTelephoneNumber += "A"
                           $InputTelephoneNumber += "B"
                           $InputTelephoneNumber[i] = $TempInputTelephoneNumber[j]
     	          If ($i -eq "7") {
	              $InputTelephoneNumber[i] = "-"	
                           }
                          If ($i -eq "3") {
                             $InputTelephoneNumber[i] = "-"
	          }
	}
            }
}
Write-Host "$($InputTelephoneNumber)"

Open in new window

0
Comment
Question by:namerg
6 Comments
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 250 total points
ID: 38769554
Try...
[long]$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
[string]$InputTelephoneNumber = "{0:###-###-####}" -f $InputTelephoneNumber
$InputTelephoneNumber

Open in new window

0
 
LVL 4

Expert Comment

by:Smighty
ID: 38769973
why not simply input them in the E.164 format? The dashes are only used for a more convenient display of the Number...
0
 

Author Comment

by:namerg
ID: 38770483
@Subsun: Almost perfect. :) But, If i type it as 303-303-3333 I get the following error:

Cannot convert value "303-303-3333" to type "System.Int64". Error: "Input string was not in a correct format."
At C:\scripts\AD\loops.ps1:27 char:1
+ [long]$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

Open in new window


@Smighty. I am sorry, hmm...What is E.164 format ?

Thanks,
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:Smighty
ID: 38770580
E.164 is the international phone number format starting with a plus, you know: +44 303 3333
http://en.wikipedia.org/wiki/E.164
(spaces only for demonstration).
Programs Like Lync, Exchange, etc. use this format to store and dial numbers...
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 38770768
I would not cast the input to long immediately. Instead, a string should get read in, and checked for some of the common formats:
303-444-5555
303 444 5555
(303) 444 5555
(303) 444-5555
and similar. So removing any delimiter, and then reformatting to the the desired format:
$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
Write-Host "$($InputTelephoneNumber)"
$InputTelephoneNumber = '{0:###-###-####}' -f [long]($InputTelephoneNumber -Replace '[ ()/-]')

Open in new window

Processing optional E.164 input would need some more effort here.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38771507
If you are unsure about the format of input then you can use..
$InputTelephoneNumber = ($InputTelephoneNumber -replace("\D")).Insert(3,"-").Insert(7,"-")

Open in new window

\D will replace all non digits and incert will incert the "-".
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now