Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Oct 9, 2003

Email Validation Function

By DatabaseJournal.com Staff



>>Script Language and Platform: MSSQL
This function checks for invalid characters, and some basic format. Since SQL can't use regular expressions, it cannot do a thorough validation, but this function DOES handle the minimum. If the email is valid, it returns 1, if not 0.

*ex. This is to delete invalid emails

DELETE FROM address WHERE master.dbo.EMAILVALIDATE(email) = 0

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


Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose... Disclaimer Continued


Back to Database Journal Home



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM