Make sure a variable contains only 3 digits each from 0-9

I’m after some help (again) I’m getting a text file updated with a port number by a gui box for the end user. I only want a max of 3 digits in totally and each one can only range from 0-9. What can i do ? the \d says its a digit …got that far !

This looks like it might work…

“^\d{3}$”

Cracked it !!!

do {
$AAHelpDirectory = 'C:\AAHelp\tmp\port.cfg'
$CurrentPort = get-content "C:\AAHelp\tmp\port.cfg"
[int]$NewPortNumber = [Microsoft.VisualBasic.Interaction]::InputBox("`nPort is currently set to $CurrentPort `n`n`n`n`nPlease Enter the new port number", "AAHELP PORT MANAGER") 
   }
Until ($NewPortNumber -match "^\d{3}$")

The final script looked like this, if your interested ! :slight_smile:

 
 
#Add .NET assembly for GUI box  
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null

#Run 
do {
#Set AAHelp port.cfg location
$AAHelpDirectory = 'C:\AAHelp\tmp\port.cfg'
#Check current port number
$CurrentPort = get-content "C:\AAHelp\tmp\port.cfg"
#Run GUI box and request new 3 digit number
[int]$NewPortNumber = [Microsoft.VisualBasic.Interaction]::InputBox("`nPort is currently set to $CurrentPort `n`n`n`nPlease Enter the new port number `n(ONLY 3 Digits)", "AAHELP PORT MANAGER") 
   }
#Loop until 3 digit number is supplied
Until ($NewPortNumber -match "^\d{3}$")

#Update port.cfg file
Set-Content -Path $AAHelpDirectory -Value $NewPortNumber