the reason your version fails is that you changed the basic structure of the while
test from your source. plus, you ignored the fact that .StartsWith()
and .EndsWith()
are both case sensitive. that last means that domain
and Domain
are NOT the same thing. [grin]
instead of untangling that, i rewrote the idea in a way that seems more obvious to me.
what it does ...
- creates the constants
- creates regex-escaped versions of the test strings
the one that ends with a slash won't work without escaping since that is a reserved character. the one that ends with .com
won't always work without escaping since the dot is "any character" in the regex pattern language.
- sets the input variable to a blank string
- starts a
while
loop
- test for a
null or empty
string
- gets the user input
if
tests for a domain id
elseif
tests for an email address
else
writes a warning & sets the input $Var to a blank string
- after the while loop exits, shows the content of the
$RHInput
variable
the code ...
$DomainName = 'Ziggity'
$EmailSuffix = '@{0}.com' -f $DomainName
$ES_Regex = [regex]::Escape($EmailSuffix)
$UserNamePrefix = '{0}\' -f $DomainName
$UNP_Regex = [regex]::Escape($UserNamePrefix)
$RHInput = ''
while ([string]::IsNullOrEmpty($RHInput))
{
$RHInput = Read-Host ('Enter an ID [ UserName@{0} or {1}UserName ] ' -f $EmailSuffix, $UserNamePrefix)
if ($RHInput -match "^$UNP_Regex")
{
'You entered a domain ID [ {0} ]' -f $RHInput
}
elseif ($RHInput -match "$ES_Regex$")
{
'You entered an email address [ {0} ]' -f $RHInput
}
else
{
Write-Warning ( '[ {0} ] is not a valid email address OR a valid domain ID.' -f $RHInput)
$RHInput = ''
}
}
''
$RHInput
output while in the while
...
# 1st run thru
Enter an ID [ UserName@@Ziggity.com or Ziggity\UserName ] : testing@ziggity.com
You entered an email address [ testing@ziggity.com ]
# 2nd run thru
Enter an ID [ UserName@@Ziggity.com or Ziggity\UserName ] : testing\tutu
WARNING: [ testing\tutu ] is not a valid email address OR a valid domain ID.
Enter an ID [ UserName@@Ziggity.com or Ziggity\UserName ] : ziggity\testing
You entered a domain ID [ ziggity\testing ]
output after the while
loop ...
# 1st run thru
testing@ziggity.com
# 2nd run thru
ziggity\testing