Same function with check for white spaces as well.
function Do-Thing {
param(
[ValidateNotNullOrEmpty()]
$FirstName,
[ValidateNotNullOrEmpty()]
$LastName,
[ValidateNotNullOrEmpty()]
$Company,
[ValidateNotNullOrEmpty()]
[Validatepattern('^\S*$')]
$EmailAddress,
)
# Do things here
}
Aww, I thought the css colored text would work (copy and paste from vscode). And these windows are quite narrow (62 characters). Anyway, here’s my version, with all my tricks to bear.
I hate to be a stickler, but an email address can have a space in it. What will happen is that the space is read as a delimiter between the DisplayName and the other components of the email address (User, Host, Address)
My suggestion is to use the function I wrote Test-IsValidEmailAddress taken from inspiration I found on the Internet with a whole lot more logic and comment based help.
Function Test-IsValidEmailAddress {
[CmdletBinding()]
[Outputtype([bool])]
Param (
[parameter(Mandatory=$True,Position=0,ValueFromPipeLine=$True,ValueFromPipeLineByPropertyName=$True)]
[Alias("Address")]
[string] $EmailAddress
)
Process {
Write-Verbose -message "You entered email address: [$($EmailAddress)]"
Try {
$temp = [System.Net.Mail.MailAddress] $EmailAddress
write-verbose -message "Address resolved to: [$($temp.Address)]"
if ($temp.Address -ne $EmailAddress) {
write-verbose -message "[$($temp.Address)] does not match [$($EmailAddress)]"
write-ouput $false
} else {
Write-Verbose -message "Address valid, no guarantee that address [$($EmailAddress)] exists."
Write-Output $True
}
} Catch {
Write-Verbose -message "The address is NOT valid."
Write-Output -inputobject $False
}
}
} #EndFunction Test-IsValidEmailAddress