Validating e-mail addresses

This topic contains 4 replies, has 3 voices, and was last updated by  Cyril Muffat 2 years, 1 month ago.

  • Author
    Posts
  • #31118

    Vandrey Trindade
    Participant

    Hi,

    I have found this function to validate some mail addresses from a list>


    function Validate-Mail{
    Param ([String]$Address)
    ($Address -as [System.Net.Mail.MailAddress]).Address `
    -eq $Address -and $Address -ne $null}

    But it only works for mail addresses like this:
    test@@aol.com
    test.gmail.com

    And I want to add this kind of mail address in the block list:
    informações@uol.com.br

    Since "ç" and "õ" aren't used on mail addresses.
    Need to find a way to block any address with graphic accentuations.

  • #31119

    Mark Hammonds
    Participant

    you will need to use a regular expression (Regex) let me do some looking for you.

    if($email -match "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"){"true"}
    else{"False"}

    http://www.regular-expressions.info/email.html

  • #31121

    Mark Hammonds
    Participant

    Maybe this

    function Validate-Mail{
    Param(
    
           [ValidatePattern(""^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$")]
    
           ($Address -as [System.Net.Mail.MailAddress]).Address `
    -eq $Address -and $Address -ne $null}
    
     )
    
    
  • #31144

    Vandrey Trindade
    Participant

    Mark Hammonds,

    Thanks! The first one worked perfectly =]


    if($email -match "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"){"true"}
    else{"False"}

  • #31146

    Cyril Muffat
    Participant

    the long version version of RFC 1035 is :

    \A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*
    | "(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]
    | \\[\x01-\x09\x0b\x0c\x0e-\x7f])*")
    @ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
    | \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
    (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
    (?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]
    | \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)
    \])\z

    "scream" !

    but the short :

    \A[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@
    (?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z

    or above works in 99% of cases

You must be logged in to reply to this topic.