SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


E-Mail Validator


E-Mail Validator

Author
Message
Sean Smith (SSC)
Sean Smith (SSC)
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1474 Visits: 1012
Comments posted to this topic are about the item E-Mail Validator
Dennis Wagner-347763
Dennis Wagner-347763
SSC Veteran
SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)

Group: General Forum Members
Points: 272 Visits: 228
Very nice.

I would make a couple of suggestions though. To avoid the loop looking at each individual character, use regular expressions to look for invalid characters:

IF (
@vParameter_Length = 0
OR CHARINDEX ('@', @vParameter) < 2
OR CHARINDEX ('.', @vParameter) < 2
OR CHARINDEX ('.@', @vParameter) <> 0
OR CHARINDEX ('..', @vParameter) <> 0
OR CHARINDEX ('.', REVERSE (@vParameter)) < 3
OR CHARINDEX ('.', @vParameter, CHARINDEX ('@',@vParameter)) = 0
OR RIGHT (@vParameter,1) = '@'
OR @vParameter LIKE '%@%@%'
OR @vParameter LIKE '%[!-*]%'
OR @vParameter LIKE '%,%'
OR @vParameter LIKE '%/%'
OR @vParameter LIKE '%[:-?]%'
OR @vParameter LIKE '%[[-^]%'
OR @vParameter LIKE '%`%'
OR @vParameter LIKE '%[{-~]%'

)

Then if you are going to do it on an entire table, you can avoid the function altogether which would probably bring your SQL Server to its knees to have a function in the where clause of a select against a million row table. This query ran against an 800,000+ row table in about 4 seconds.
cfrestrepo
cfrestrepo
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 610
I found this code in Internet some time ago.

I modify to Sqlserver


create FUNCTION IsValidEmail
(@Email varchar(100))



RETURNS bit
AS
BEGIN

Declare @Long Int,
@Pos Int,
@Bueno Bit,
@Ext varchar(1000),
@Letras varchar(100),
@I Int

Set @Letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ@.-_0123456789'
Set @Email = rtrim(ltrim(@Email))
Set @Long = Len(@Email)
Set @Pos = CharIndex('@',@Email)
Set @Bueno = 0

If not (@Long < 6 -- Muy corto (very short)
Or @Pos = 0 -- No hay arrobas (no @s)
Or CharIndex('@',@Email,CharIndex('@',@Email)+1) > 0 -- dos o mas arrobas (more than 1 @)
Or CharIndex(left(@Email,1),'@.-_') > 0 -- No empieza por '@.-_'
Or CharIndex(right(@Email,1),'@.-_') > 0 -- No termina en '@.-_'
Or CharIndex('@.',@Email) > 0
or CharIndex('..',@Email) > 0
or CharIndex('.@',@Email) > 0) -- @. ó ..

Begin
Set @Ext = '.COM.EDU.GOV.NET.BIZ.ORG.TV.INFO'
+ '.AF.AL.DZ.As.AD.AO.AI.AQ.AG.AP.AR.AM.AW.AU.AT.AZ.BS.BH.BD.BB.BY'
+ '.BE.BZ.BJ.BM.BT.BO.BA.BW.BV.BR.IO.BN.BG.BF.MM.BI.KH.CM.CA.CV.KY'
+ '.CF.TD.CL.CN.CX.CC.CO.KM.CG.CD.CK.CR.CI.HR.CU.CY.CZ.DK.DJ.DM.DO'
+ '.TP.EC.EG.SV.GQ.ER.EE.ET.FK.FO.FJ.FI.CS.SU.FR.FX.GF.PF.TF.GA.GM.GE.DE'
+ '.GH.GI.GB.GR.GL.GD.GP.GU.GT.GN.GW.GY.HT.HM.HN.HK.HU.IS.IN.ID.IR.IQ'
+ '.IE.IL.IT.JM.JP.JO.KZ.KE.KI.KW.KG.LA.LV.LB.LS.LR.LY.LI.LT.LU.MO.MK.MG'
+ '.MW.MY.MV.ML.MT.MH.MQ.MR.MU.YT.MX.FM.MD.MC.MN.MS.MA.MZ.NA'
+ '.NR.NP.NL.AN.NT.NC.NZ.NI.NE.NG.NU.NF.KP.MP.NO.OM.PK.PW.PA.PG.PY'
+ '.PE.PH.PN.PL.PT.PR.QA.RE.RO.RU.RW.GS.SH.KN.LC.PM.ST.VC.SM.SA.SN.SC'
+ '.SL.SG.SK.SI.SB.SO.ZA.KR.ES.LK.SD.SR.SJ.SZ.SE.CH.SY.TJ.TW.TZ.TH.TG.TK'
+ '.TO.TT.TN.TR.TM.TC.TV.UG.UA.AE.UK.US.UY.UM.UZ.VU.VA.VE.VN.VG.VI'
+ '.WF.WS.EH.YE.YU.ZR.ZM.ZW'


Set @Pos = @Long + 1 - CharIndex('.',Reverse(@Email)) -- localiza el último punto (locate last period)

If CharIndex(substring(@Email, @Pos,60), @Ext) > 0 -- Chequea terminación válida
Begin
Set @I = 1
While @I <= @Long
Begin
If CharIndex(Substring(@Email,@I,1),@Letras) = 0 -- Letra o caracter inválido (invalid char)
Break

Set @I = @I + 1
End
End

If @Long < @I
Set @Bueno = 1

End

Return @Bueno



Sean Smith (SSC)
Sean Smith (SSC)
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1474 Visits: 1012
Very nice! Thanks for all of the helpful tips and tricks. Smile
JediSQL
JediSQL
Old Hand
Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)

Group: General Forum Members
Points: 382 Visits: 213
Dennis,
I like your pattern matching approach. However, I was concerned about the 128 extended characters, so I suggest the last condition be modified to changed the ~ to a ÿ, like this:

OR @vParameter LIKE '%[{-ÿ]%'

Sincerely,
Daniel
JediSQL
JediSQL
Old Hand
Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)

Group: General Forum Members
Points: 382 Visits: 213
Oops, ÿ doesn't work

Sincerely,
Daniel
JediSQL
JediSQL
Old Hand
Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)Old Hand (382 reputation)

Group: General Forum Members
Points: 382 Visits: 213
OK, here's the fix for ÿ:

OR @vParameter COLLATE Latin1_General_BIN LIKE '%[{-ÿ]%'

Sincerely,
Daniel
Libby
Libby
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 Visits: 248
Thanks Sean! I believe this is a lot more inclusive than the edits I’d coded in my script.
Libby Montgomery
Sean Smith (SSC)
Sean Smith (SSC)
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1474 Visits: 1012
Thanks. Hope it helps.
Iwas Bornready
Iwas Bornready
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22602 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search