## Email Validation FunctionOctober 9, 2003
Author: Jimmy Hattori CREATE FUNCTION EMAILVALIDATE (@email varChar(100)) RETURNS int AS BEGIN DECLARE @invalChars varchar(5),@valid int,@badChar varchar(1),@atPos int,@periodPos int SET @valid = 1 SET @invalChars = ' /:,;' --Check to see if it's blank IF len(ltrim(rtrim(@email))) = 0 SET @valid = 0 ELSE --Loop invalid characters to see if it exists in email WHILE len(@invalChars) > 0 BEGIN SET @badChar = substring(@invalChars,1,1) IF(charindex(@badChar,@email) > 0) --If invalid character was found, return 0 to invalidate SET @valid = 0 SET @invalChars = replace(@invalChars,@badChar,'') END --Check to see if "@" exists. SET @atPos = charindex('@',@email,1) IF @atPos = 0 SET @valid = 0 --Check to see if extra "@" exists after 1st "@". IF charindex('@',@email,@atPos+1) > 0 SET @valid = 0 SET @periodPos = charindex('.',@email,@atPos) IF @periodPos = 0 SET @valid = 0 IF (@periodPos+3) > len(@email) SET @valid = 0 RETURN (@valid) END
Back to |