Solved

How to add (-) to a phone number ?

Posted on 2013-01-11
6
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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