if object_id('fnCheckEmail') is not null
drop function fnCheckEmail
GO
----創(chuàng )建驗證函數,返回值為1表示正確,否則格式錯誤
create function fnCheckEmail(@Email varchar(1000))
returns bit
as
begin
declare @rtv bit
if
charindex(' ',@email) > 0 or /*含有空格*/
len(@email) - len(replace(@email,'.','')) > 1 or /*'.'超過(guò)1個(gè)*/
len(@email) - len(replace(@email,'@','')) > 1 or /*'@'超過(guò)1個(gè)*/
right(@email,1) = '.' or /*以'.'結尾*/
right(@email,1) = '@' or /*以'@'結尾*/
left(@email,1) = '.' or /*以'.'開(kāi)頭*/
left(@email,1) = '@' or /*以'.'開(kāi)頭*/
charindex('.',@email)-charindex('@',@email) < 0 or /*'.'在'@'前面*/
charindex('@',@email)-charindex('.',@email) = 1 or /*'@'與'.'相鄰*/
charindex('.',@email)-charindex('@',@email) = 1 /*'@'與'.'相鄰*/
set @rtv = 0
else
set @rtv = 1
return @rtv
end
GO
----創(chuàng )建測試數據
declare @t table(email varchar(1000))
insert @t
select 'ab.cxyz@s.com' union all
select 'ab@xyz@s.com' union all
select '@abc@xyz.com' union all
select 'abcxyz.com@' union all
select '.abcxyz@com' union all
select 'abc@xyz@com.' union all
select 'ab.c@xyzcom' union all
select 'abc@.com' union all
select 'abc@xyz.com' union all /*格式正確*/
select 'ab c@xyzcom'
----驗證
select *,case dbo.fnCheckEmail(email) when 1 then '正確' else '錯誤' end from @t
----清除測試環(huán)境
drop function fnCheckEmail
聯(lián)系客服