How can ensure that a function only accepts a positive integer?

a_m_2016

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.

Joey

You can use ValidateRange for the parameter:

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

From the documentation:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

How do I display an integer with a function that only accepts a char in C?

From Dev

How to ensure a textbox only accepts integers (and not string)

From Dev

How can I define an Elisp widget type that only accepts positive numbers?

From Dev

How to write a function that accepts a tuple that contains integer elements?

From Dev

My function returns a list with a single integer in it, how can I make it return only the integer?

From Dev

bash allow only positive integer or positive integer with decimal point

From Dev

Is there a way to make GCC alert me when I provide a signed integer to function that accepts only unsigned one?

From Dev

How can I create a PasswordBox with an input scope that accepts only numbers?

From Dev

How can I make a TextBox accepts only numeric values?

From Java

How to ensure only one Azure Function BlobTrigger runs at a time?

From Dev

How to ensure a code execution only after the await-function is done?

From Dev

How to ensure that a method can only be called from a specific dll

From Dev

How can I ensure that only one if a kind of Jenkins job is run?

From Dev

How to ensure that new instance of class can be created only by other class?

From Dev

How can I ensure that only one if a kind of Jenkins job is run?

From Dev

How can I safely ensure a variable contains only a valid filename?

From Dev

How to ensure each Controller can see the model and is only initialized once?

From Dev

Generic Sorting function accepts T, but want to ensure T is comparable

From Dev

How can I ensure a reactjs state is updated, and then call a function?

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

How can I ensure that my function is being called and running

From Dev

How can EditText accept only integer

From Dev

Positive integer from Python hash() function

From Dev

how to ensure irb accepts emoji input instead of escaping it?

From Dev

How to write a function that takes a positive integer N and returns a list of the first N natural numbers

From Dev

Given a positive integer, n, how can a numerical triangle of height n-1 be printed?

From Dev

How to navigating only by positive tabindex

Related Related

  1. 1

    How can ensure that a function only accepts a positive integer?

  2. 2

    How do I display an integer with a function that only accepts a char in C?

  3. 3

    How to ensure a textbox only accepts integers (and not string)

  4. 4

    How can I define an Elisp widget type that only accepts positive numbers?

  5. 5

    How to write a function that accepts a tuple that contains integer elements?

  6. 6

    My function returns a list with a single integer in it, how can I make it return only the integer?

  7. 7

    bash allow only positive integer or positive integer with decimal point

  8. 8

    Is there a way to make GCC alert me when I provide a signed integer to function that accepts only unsigned one?

  9. 9

    How can I create a PasswordBox with an input scope that accepts only numbers?

  10. 10

    How can I make a TextBox accepts only numeric values?

  11. 11

    How to ensure only one Azure Function BlobTrigger runs at a time?

  12. 12

    How to ensure a code execution only after the await-function is done?

  13. 13

    How to ensure that a method can only be called from a specific dll

  14. 14

    How can I ensure that only one if a kind of Jenkins job is run?

  15. 15

    How to ensure that new instance of class can be created only by other class?

  16. 16

    How can I ensure that only one if a kind of Jenkins job is run?

  17. 17

    How can I safely ensure a variable contains only a valid filename?

  18. 18

    How to ensure each Controller can see the model and is only initialized once?

  19. 19

    Generic Sorting function accepts T, but want to ensure T is comparable

  20. 20

    How can I ensure a reactjs state is updated, and then call a function?

  21. 21

    How can I ensure that correct function is called in case there are multiple candidates

  22. 22

    How can I ensure that correct function is called in case there are multiple candidates

  23. 23

    How can I ensure that my function is being called and running

  24. 24

    How can EditText accept only integer

  25. 25

    Positive integer from Python hash() function

  26. 26

    how to ensure irb accepts emoji input instead of escaping it?

  27. 27

    How to write a function that takes a positive integer N and returns a list of the first N natural numbers

  28. 28

    Given a positive integer, n, how can a numerical triangle of height n-1 be printed?

  29. 29

    How to navigating only by positive tabindex

HotTag

Archive