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(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[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.
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.
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?!
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
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.
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