In PowerSell, how can I include the necessary code, to the code below, that would reject any input that consists entirely of numbers/digits, but would accept alpha/alphanumeric values with or without hyphens, but NOT spaces or periods (.)?
DO {
$NewID = Read-Host -Prompt " NEW ID NAME of object (8-15 chars) "
} UNTIL ($NewID.Length -gt 7 -and $NewName.Length -lt 16)
if($foo -notmatch '^\d+$' -and $foo -match '^\w[\w-]*$') { ...ok } else { ...bad }
Example:
@( "123", "123 T", "123T", "123-T-456", "123 T 456" ) |
foreach-object {
if( $_ -notmatch '^\d+$' -and $_ -match '^\w[\w-]*$') { "'$_' passed" }
else { "'$_' failed" }
}
Results:
'123' failed
'123 T' failed
'123T' passed
'123-T-456' passed
'123 T 456' failed