Need help with ValidatePattern

Welcome Forums General PowerShell Q&A Need help with ValidatePattern

This topic contains 7 replies, has 4 voices, and was last updated by

 
Participant
4 years, 11 months ago.

  • Author
    Posts
  • #13099

    Participant
    Points: 0
    Rank: Member

    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

  • #13100

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    Out of curiosity, have you added [CmdletBinding()] just above the param() block?

  • #13101

    Participant
    Points: 0
    Rank: Member

    Hi Don Jones

    Thank you for helping me, I tried that as well but no avail.

  • #13102

    Participant
    Points: 0
    Rank: Member

    I got it right when i used single quote instead of double quotes, However it works in both ways with PS 3.0 !

    [ValidatePattern('^\d{4}$')]

    Thank you

  • #13103

    Member
    Points: 0
    Rank: Member

    Looks like that trailing $ causes problems in PowerShell v2, even though it doesn't actually cause any expansion to take place. It worked when I put a backtick before the $ (in a double-quoted string). In this case, though, single quotes are more appropriate anyway.

  • #13105

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    Ah, beat me to it. Ya, dug out a v2 VM and observed the same thing. Strikes me as a bug, which is obviously fixed, but a search in Connect didn't turn anything up. Likely the team noticed it and fixed it internally for v3.

    Anyway, Nithun, there's a kind of "good practice" to use single quotes unless you explicitly need the feature of double quotes (variable replacement, escape characters, subexpressions), which helps avoid this problem (as Dave suggested).

  • #13106

    Participant
    Points: 0
    Rank: Member

    Thank you so much Don and Dave for the inputs, I am very new to powershell.org and it was my first post here.
    Feel so special and blessed to be a part of this.

    Good day
    Nithun

  • #13109

    Participant
    Points: 14
    Rank: Member

    http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=3447&m=3441&ct=18346#?fbid=SD-xryBRFvI

    Go to this video and jump to the 19:24 mark or they have it bookmarked on the right.

    Even better watch the whole video > even better the whole series is so awesome and has helped me greatly!

    -VERN

The topic ‘Need help with ValidatePattern’ is closed to new replies.