Solved

How to add (-) to a phone number ?

Posted on 2013-01-11
6
299 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 69

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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