Jee
Jee

Reputation: 71

PowerShell Input Validation - Input should NOT be ALL numbers

I have the following code that works well for validating length...

DO {
    $NewID = Read-Host -Prompt " NEW ID NAME of object (8-15 chars)   "
} UNTIL ($NewID.Length -gt 7 -and $WS_NewName.Length -lt 16)

How can I include code that ensures input contains either an ALPHA or ALPHANUMERIC string, but NOT a purely NUMERIC one?

Upvotes: 0

Views: 231

Answers (1)

Robert Dyjas
Robert Dyjas

Reputation: 5227

This can be easily doable using regular expressions like that:

($NewID -match '^[A-z0-9]*$') -and ($NewID -notmatch '^[0-9]*$')

Short explanation: first expression looks for alpha/alphanumeric string and the second discards purely numeric entries.

By the way, in your example you use $NewID and then $WS_NewName in Until expression, that might be confusing (however, I assume you just forgot to change it while pasting here)

Upvotes: 1

Related Questions