# Allow input 0 or at least a 10 digit number

• ### Question

• Dear All,

For 2 days, I am trying to figure out, how I can handle an input regarding a phone number.

Here a simplified code, that hopefully is understandable:

if (\$Phonenumber.Text -ne 0 -or \$Phonenumber.TextLength -le 10)  {[System.Windows.MessageBox]::Show("Telephone Number is too short, please correct","Ooops",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Warning)}

My wish is, that a user requested to enter a phone number, can either enter a 0 (if there is no phone number available) or can enter a phone number, but it needs to be at least 10 digits.

Here my logic does not work, because 0 is not the required length and therefore I get an error.

I tried different approaches with different operators but always failed.

Maybe you laugh and have a trick up your sleeve or a workaround?

Thank you very much for your help,

Mike

Saturday, November 17, 2018 12:14 AM

• Not!

```\$Phonenumber = Read-Host -Prompt 'Please enter a Phone Number "999-999-9999"'
if(\$Phonenumber -match '\d{3,3}-\d{3,3}-\d{4,4}\$'){
Write-Host good phone number
}elseif(\$Phonenumber -eq '0'){
Write-Host user opted out

\_(ツ)_/

• Edited by Saturday, November 17, 2018 12:39 AM
• Marked as answer by Saturday, November 17, 2018 12:44 AM
Saturday, November 17, 2018 12:34 AM
• I would suggest similar to below to meet your conditions.

```\$PhoneNumber = Read-Host -Prompt "Please enter a Phone Number"
If (((\$PhoneNumber -Match "^[0-9]+\$") -And (\$PhoneNumber.Length -eq 10)) -Or (\$PhoneNumber -eq "0"))
{
"\$PhoneNumber  is OK"
}
Else
{
"\$PhoneNumber  is INVALID"
}```

This only allows digits, exactly 10, or simply "0". Note that dashed, parentheses, spaces, etc. are not allowed.

Edit: jrv's solution is better. It requires dashes (and 10 digits).

Richard Mueller - MVP Enterprise Mobility (Identity and Access)

• Edited by Saturday, November 17, 2018 12:43 AM
• Marked as answer by Saturday, November 17, 2018 12:47 AM
Saturday, November 17, 2018 12:38 AM
• You can also do this:

if(\$Phonenumber -match '\d{10,10}\$|^0\$'){

Which will be true for exactly 10 digits or a single '0'.

\_(ツ)_/

• Marked as answer by Saturday, November 17, 2018 12:50 AM
Saturday, November 17, 2018 12:41 AM

### All replies

• Try this:

```\$Phonenumber = Read-Host -Prompt "Please enter a Phone Number"IF( \$Phonenumber -eq "0" -or \$Phonenumber.length -eq 10)

{  then entry is OK }

ELSE

{ entry is not OK }```

Mike Smith TechTrainingNotes.blogspot.com
Books: SharePoint Calculated Columns and Validation Formulas
@TechTrainNotes

Saturday, November 17, 2018 12:21 AM
• Dear Mike,

Thank you for your fast response.

\$Phonenumber.length -eq 10

So if it is equal but I try to challenge you, that the length of the number needs to be at least 10 ;)

I need something like Either it is 0 or at least the length of 10

Thank you for helping,

Mike

Saturday, November 17, 2018 12:27 AM
• Not!

```\$Phonenumber = Read-Host -Prompt 'Please enter a Phone Number "999-999-9999"'
if(\$Phonenumber -match '\d{3,3}-\d{3,3}-\d{4,4}\$'){
Write-Host good phone number
}elseif(\$Phonenumber -eq '0'){
Write-Host user opted out

\_(ツ)_/

• Edited by Saturday, November 17, 2018 12:39 AM
• Marked as answer by Saturday, November 17, 2018 12:44 AM
Saturday, November 17, 2018 12:34 AM
• I would suggest similar to below to meet your conditions.

```\$PhoneNumber = Read-Host -Prompt "Please enter a Phone Number"
If (((\$PhoneNumber -Match "^[0-9]+\$") -And (\$PhoneNumber.Length -eq 10)) -Or (\$PhoneNumber -eq "0"))
{
"\$PhoneNumber  is OK"
}
Else
{
"\$PhoneNumber  is INVALID"
}```

This only allows digits, exactly 10, or simply "0". Note that dashed, parentheses, spaces, etc. are not allowed.

Edit: jrv's solution is better. It requires dashes (and 10 digits).

Richard Mueller - MVP Enterprise Mobility (Identity and Access)

• Edited by Saturday, November 17, 2018 12:43 AM
• Marked as answer by Saturday, November 17, 2018 12:47 AM
Saturday, November 17, 2018 12:38 AM
• You can also do this:

if(\$Phonenumber -match '\d{10,10}\$|^0\$'){

Which will be true for exactly 10 digits or a single '0'.

\_(ツ)_/

• Marked as answer by Saturday, November 17, 2018 12:50 AM
Saturday, November 17, 2018 12:41 AM
• Dear Jrv,

Thank you very much for your fast response.

I tried your code and added the 0 to be allowed and that works. This looks like a promising solution. Will have a look deeper into the -match function.

As always,

Thank you very much for your help and support,

much appreciated,

Mike

if(\$Phonenumber -match '\d{3,3}-\d{3,3}-\d{4,4}' -or \$Phonenumber -eq 0){
Write-Host good phone number
}else{
}

Saturday, November 17, 2018 12:43 AM
• Thank you very much,

Richard

Saturday, November 17, 2018 12:48 AM
• This will work for all phone numbers that share the country code "1". It also screens for invalid phone numbers (e.g. area codes and exchange codes). If you want to use the regex to match phone numbers within longer text strings, just replace the "^" and "\$" anchors with "\b" anchors.

If you have a need to match international phone numbers, good luck! You'll need a lot of regex's to accomplish that!

The regex in the example lets you surround the area code with parenthesis, and to separate the parts of phone numbers with combinations of "-", ".", and spaces.

```\$p = Read-Host -Prompt "Please enter a Phone Number"
if (\$p -match ^\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})\$")
{
\$p= [String]::Join("",\$matches[1..3])
write-host "Phone number: \$p"
}
elseif (\$p -eq "0")
{
write-host "No phone number"
}
else
{
"Invalid phone number"
}```

--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

Saturday, November 17, 2018 3:34 AM