Reprompt User for invalid input

I have a script which takes lots of input from user. I need to validate those inputs. The issue I am currently facing is that if one of the input fails validation, the user should be prompted to re-enter only that particular input and rest all valid inputs should remain as is

Sample Code :

Function Validate 
{ Param( 
[ValidateLength(3,5)] [String[]]$Value ) 
$Value = Read-Host "Please enter a value" 
Validate $Value 
Write-Host $value 
$Test = Read-Host "Enter Another value" 
Write-Host $Test

Here when validation fails for $Value it throws exception and moves to take second input.

Welcome to the forum. :wave:t4:

Before we proceed … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

Hmmm … but the user will be prompted already if the input does not match the requirements you set with your inputvalidation conditions in your Param() block?! :man_shrugging:t4:

Currently it does not prompt for the same input again, it just throws the validation error on console for the first input and moves on to ask the user for second input

Ah … now I see … at least if you enter less than 3 or more than 5 characters you will be prompted to enter a value again.

You may create your own validate logic inside your function code with a loop and the logic and the conditions you want to check.

Thanks Olaf.

Seems like using a Loop is the only way to get it. I was hoping if I could avoid using loop somehow since I need to take many inputs from the user and would need a loop for each of them.

So you may create a function able to validate all of the cases you need. Can you elaborate a little more detailed what exactly you want to validate?

I am taking various types of input, so I may require basic non-null validation for all validations. Some additional validation like valid email address, numeric values only validation.

My main concern is that I don’t want to throw exception, I want to just send a basic message informing the user that the value is incorrect and then ask them to reinput the value again

If the individual input values are too different from each other it may be necessary to use individual validations.

Some requirements may be covered by standard PowerShell functions but you need something above that you will have to code it yourself.

You should read up about

Maybe pay special attention to HelpMessage argument or to the Parameter and variable validation attributes.

In general you should always read the complete help including the examples for the cmdlets you’re about to use to learn how to use them.

Thanks a lot Olaf for helping me out.

For now I have put individual While loop for validating null input.

I feel like I have reached my limits with understanding how to use Params. I will take it up maybe next week and see if I can update my While loops

1 Like