Page 1 of 1

Validar e-mail usando Regex (SOLUCIONADO)

PostPosted: Sun Feb 04, 2024 12:14 am
by Armando
Foreros:

Alguien valida la dirección de correo electrónico usando una expresión regular (Regex) y quiera compartirlo?

Saludos

Re: Validar e-mail usando Regex (SOLUCIONADO)

PostPosted: Sun Feb 04, 2024 3:58 am
by Cgallegoa
Code: Select all  Expand view

#Include "FiveWin.ch"
#include "hbcompat.ch"

Function Inicio()
MsgInfo(cValToChar(validaEmail("tuemail@tuservidor.com")))     // .T.
MsgInfo(cValToChar(validaEmail("tuemail$tuservidor.com")))      // .F.
MsgInfo(cValToChar(validaEmail("tuemail@tuservidor%com")))    // .F.
MsgInfo(cValToChar(validaEmail("tuemail@tuservidorcom")))      // .F.
MsgInfo(cValToChar(validaEmail("tuemail@tuservidor")))            // .F.
etc, etc, etc
Return(NIL)

function validaEmail(cEmail)
    LOCAL pCompiled := hb_regexComp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$")
    LOCAL aMatch
    LOCAL lRet := .t.
    aMatch = hb_regex( pCompiled, alltrim(cEmail) )
    if Empty( aMatch )
        lRet := .f.
    end
return lRet
 

Re: Validar e-mail usando Regex (SOLUCIONADO)

PostPosted: Sun Feb 04, 2024 4:00 am
by Armando
Carlos:

Muchas gracias.

Saludos