0

I am wanting to validate a user's input of special characters and if the characters are not correct, I want it to loop back to the read-host statement. I am unsure what loop type I should use and how to do this. Any and all help would be greatly appreciated.

For example

$Example = Read-Host “Input one of the following symbols 
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)"

1 Answer 1

0

Something like this should suit your needs.

do {
    $Symbol = Read-Host 'Symbol'
    if ($Symbol -match '^(\+|-|\*|/)$'){
        $Valid = $true
    } else {
        Write-Host "'$($Symbol)' is not a valid input, please choose one of +,-,* or /"
        $Valid = $false
    }
} while (!$Valid)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I really appreciate the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.