Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add (-) to a phone number ?

Posted on 2013-01-11
6
Medium Priority
?
305 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 1000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 71

Accepted Solution

by:
Qlemo earned 1000 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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

604 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