Solved

How to add (-) to a phone number ?

Posted on 2013-01-11
6
303 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 70

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

690 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