Validating Input

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus


13 Free SQL Tools from Idera


Free Ebook: Mastering Powershell

You can also use regular expressions to validate user input. For example, the following piece of code only accepts three seven digit numbers:

do {
$result = Read-Host "3-7-digit number"
$result = $result.Trim("0")

} while ($result -notmatch '^\d{3,7}$')
"Entered: $result"

Twitter This Tip! ReTweet this Tip!


Posted Feb 08 2011, 08:00 AM by ps1

Comments

mjolinor wrote re: Validating Input
on 02-08-2011 10:59 AM

For example, the following piece of code only accepts three seven digit numbers:

Shouldn't that read "only accepts one, three to seven digit number"?

Copyright 2012 PowerShell.com. All rights reserved.