Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 360 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can ensure that a function only accepts a positive integer?

#1
I want to do some basic validation on a user input in PowerShell to ensure a user can only enter a whole integer and does not enter -7 for example. I am not sure how this is done and would appreciate a pointer.

[parameter(Mandatory=$false)][int]$number

If a user enters `-$number` this will be accepted. I want it to reject this type of input.
Reply

#2
You can use `ValidateRange` for the parameter:

[parameter(Mandatory=$false)]
[ValidateRange(1, [int]::MaxValue)]
[int] $number

From the [documentation][1]:

> ValidateRange Validation Attribute

> The ValidateRange attribute specifies a numeric range for each
parameter or variable value. Windows PowerShell generates an error
if any value is outside that range. In the following example,
the value of the Attempts parameter must be between 0 and 10.

> Param
(
[parameter(Mandatory=$true)]
[ValidateRange(0,10)]
[Int]
$Attempts
)

> In the following example, the value of the variable $number must be
between 0 and 10.

> [Int32][ValidateRange(0,10)]$number = 5


[1]:

[To see links please register here]

Reply

#3
Since PowerShell 6.1.0 you kan use ValidateRangeKind to initialize the attribute:

```
[Parameter(Mandatory = $false)]
[ValidateRange("Positive")]
[Int] $Number = 5

```

> ValidateRange validation attribute
>
> The ValidateRange attribute specifies a numeric range or a ValidateRangeKind enum value for each parameter or variable value. PowerShell generates an error if any value is outside that range.
>
> The ValidateRangeKind enum allows for the following values:
>
> - Positive - A number greater than zero.
> - Negative - A number less than zero.
> - NonPositive - A number less than or equal to zero.
> - NonNegative - A number greater than or equal to zero.
Reply

#4
In Powershell 5
```
[Parameter(Mandatory = $false)]
[ValidateScript({$_ -gt 0})]
[Int] $Number = 5
```
Reply

#5
If you want to include the zero, you might simply use the [`[uint]` or `[ulong]`](

[To see links please register here]

(which also give you a larger range of positive numbers):

[parameter(Mandatory=$false)][uint]$number
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through