0

I need the user to input a title of a book.

I need to make sure they input string only.

This is where I'm at so far, any guidance please.

Do { $strTitle = Read-host "Enter the book title"} while ($strTitle -eq "")

Musa
  • 553
  • 1
  • 7
  • 19

2 Answers2

1

What do you mean by string? alpha characters only?

You could try a regular expressions.

Regular Expression to match only alphabetic characters

http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular-expressions-are-your-friend-part-1.aspx

Community
  • 1
  • 1
Paul Rowland
  • 8,244
  • 12
  • 55
  • 76
0

Code to check for alpha characters:

Do {
    $strTitle = Read-host "Enter the book title"
} until ($strTitle -notMatch "[^[:alpha:]]")
Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
  • I used this method but I was stuck in an endless loop. I wanted to setup more of a regular expressions, just don't understand the setup so far I have - [regex]$RstrTitle = “^[a-z]$” – Musa Mar 05 '14 at 22:55
  • Woops, sorry, The loop should actually be a `do {} until ()` loop, not `while`. That was why it was looping forever. – Vasili Syrakis Mar 05 '14 at 23:17
  • 1
    The only two output types from this command are String and SecureString (according to the help on Read-Host). And you only get SecureString if you specify `-AsSecureString`. – Keith Hill Mar 06 '14 at 04:43
  • I've changed the code so that it expects only alpha characters using the POSIX class `[:alpha:]` – Vasili Syrakis Mar 06 '14 at 04:52