Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Need help with ValidatePattern

$
0
0

I am trying to validate user input using ValidatePattern

param (
[Parameter(Position=0,mandatory=$true)] # Parameter is mandatory
[ValidatePattern("^\d{4}$")]
[int]$UserIp
)

$GtVer = $UserIp.ToString()

$GtVer

This code worked perfectly fine in PS 3.0 but failed in 2.0 throwing below error. How can i achieve this in PS 2.0 ?

Parameter attributes need to be a constant or a script block.
At D:\Scripts\Paramv.ps1:3 char:39
+ [ValidatePattern("^\d{4}$" <<<< )]
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : ParameterAttributeArgumentNeedsToBeConstantOrScriptBlock


Viewing all articles
Browse latest Browse all 13067

Trending Articles