Created
February 6, 2020 16:01
-
-
Save a-bode/eca026910738fb5189f0a596f72a973c to your computer and use it in GitHub Desktop.
PowerShell: Validate Email or IPv4 Address using Regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Address { | |
[CmdletBinding(DefaultParameterSetName = "EmailAddress")] | |
param ( | |
[parameter(ParameterSetName = "EmailAddress")] $EmailAddress, | |
[parameter(ParameterSetName = "IPv4")] $IPv4 | |
) | |
if ($EmailAddress) { | |
$patern = '^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$' | |
$DidMatch = $EmailAddress -match $patern | |
} | |
if ($IPv4) { | |
$patern = '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$' | |
$DidMatch = $IPv4 -match $patern | |
} | |
if ($DidMatch) { | |
return $true | |
} | |
else { | |
return $false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment