# Need help with ValidatePattern

**URL:** https://forums.powershell.org/t/need-help-with-validatepattern/2264
**Category:** PowerShell Help
**Created:** [February 13, 2014, 7:07am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264 "2014-02-13T07:07:11Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![nithun-damodaran](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nithun-damodaran](https://forums.powershell.org/u/nithun-damodaran)
#### Post date: [February 13, 2014, 7:07am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/1 "2014-02-13T07:07:11Z")

</div>

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}$" &lt;&lt;&lt;&lt; )]

```

  - CategoryInfo : ParserError: (🙂 , ParseException
  - FullyQualifiedErrorId : ParameterAttributeArgumentNeedsToBeConstantOrScriptBlock

---

<div class="post-metadata">

### Author: ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)
#### Post date: [February 13, 2014, 7:10am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/2 "2014-02-13T07:10:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nithun-damodaran](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nithun-damodaran](https://forums.powershell.org/u/nithun-damodaran)
#### Post date: [February 13, 2014, 7:17am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/3 "2014-02-13T07:17:26Z")

</div>

Hi Don Jones

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

---

<div class="post-metadata">

### Author: ![nithun-damodaran](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nithun-damodaran](https://forums.powershell.org/u/nithun-damodaran)
#### Post date: [February 13, 2014, 8:05am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/4 "2014-02-13T08:05:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [February 13, 2014, 9:06am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/5 "2014-02-13T09:06:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)
#### Post date: [February 13, 2014, 9:27am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/6 "2014-02-13T09:27:05Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![nithun-damodaran](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nithun-damodaran](https://forums.powershell.org/u/nithun-damodaran)
#### Post date: [February 13, 2014, 10:27am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/7 "2014-02-13T10:27:21Z")

</div>

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

Good day  
Nithun

---

<div class="post-metadata">

### Author: ![vern\_anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vern_anderson/32/5140_2.png) [@vern\_anderson](https://forums.powershell.org/u/vern_anderson)
#### Post date: [February 14, 2014, 1:25am UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/8 "2014-02-14T01:25:17Z")

</div>

[http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=3447&amp;m=3441&amp;ct=18346#?fbid=SD-xryBRFvI](http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=3447&amp;m=3441&amp;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

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/need-help-with-validatepattern/2264/9 "2024-05-16T20:52:25Z")

</div>


