# Passing empty string '' to function sends '0'

**URL:** https://forums.powershell.org/t/passing-empty-string-to-function-sends-0/10361
**Category:** PowerShell Help
**Created:** [March 22, 2018, 10:45am UTC](https://forums.powershell.org/t/passing-empty-string-to-function-sends-0/10361 "2018-03-22T10:45:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mlanderos](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mlanderos](https://forums.powershell.org/u/mlanderos)
#### Post date: [March 22, 2018, 10:45am UTC](https://forums.powershell.org/t/passing-empty-string-to-function-sends-0/10361/1 "2018-03-22T10:45:15Z")

</div>

Spoke too soon. Im trying something else.

Thanks!

---

<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: [March 22, 2018, 11:03am UTC](https://forums.powershell.org/t/passing-empty-string-to-function-sends-0/10361/2 "2018-03-22T11:03:20Z")

</div>

PowerShell does love its coercion. So, know that your ValidatePattern isn’t helping; that forces the input into a string in order to do a regex match. [int] along makes sure you get an integer, and you can use a ValidateRange if you need to limit to, say, 100 to 999. Aside from that, in PowerShell’s mind, an empty string _is the same as zero_. I’m not sure there’s a declarative way to change it’s mind on that, unless I’m misunderstanding what you’re after.

---

<div class="post-metadata">

### Author: ![mlanderos](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mlanderos](https://forums.powershell.org/u/mlanderos)
#### Post date: [March 22, 2018, 11:58am UTC](https://forums.powershell.org/t/passing-empty-string-to-function-sends-0/10361/3 "2018-03-22T11:58:03Z")

</div>

I see. That makes sense. I just want to ensure that a terminating failure occurs if someone tries to pass an empty string ‘’ or " ", so after reading what you replied with I modified the function as such and all current functionality remained intact along with failing for passing in those empty values. 🙂

I had to add the ValidateScript validation to catch passing in " ". The ValidateNotNullOrEmpty did not catch that but It did catch passing in ‘’ or “”.

```
function Add-Increment {
    [CmdletBinding()]
    param(
    
    [ValidateNotNullOrEmpty()]
        [ValidateScript({ if([string]::IsNullOrWhiteSpace($_))
        {
            throw "The argument is null or whitespace. Provide and argument that is not null or whitespace, and then try the command again"
        } })]

    $Number

    )
    
    Write-Verbose -Message ("Number {0} was passed into function" -f $Number)

    $Number++

    Write-Verbose -Message ("After increment, Number is: {0}" -f $Number)

    $CharCount = $(($Number | Measure-Object -Character).Characters)

    if($CharCount -gt 3)
    {
        throw "Number suffix $Number surpassed 3 digits!"
    }
    else 
    {
        $Suffix = $Number.ToString("000")
    }

    Write-Verbose -Message ("Suffix to be used: {0}" -f $Suffix)
    return $Suffix
}
```

Thanks, Don!

---

<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:36pm UTC](https://forums.powershell.org/t/passing-empty-string-to-function-sends-0/10361/4 "2024-05-16T20:36:10Z")

</div>


