您的位置:首页 > 数据库

ms sql server中检测邮件地址的函数

2006-09-05 21:41 330 查看
if object_id('fnCheckEmail') is not null
drop function fnCheckEmail
GO
----创建验证函数,返回值为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 /*'.'超过1个*/
len(@email) - len(replace(@email,'@','')) > 1 or /*'@'超过1个*/
right(@email,1) = '.' or /*以'.'结尾*/
right(@email,1) = '@' or /*以'@'结尾*/
left(@email,1) = '.' or /*以'.'开头*/
left(@email,1) = '@' or /*以'.'开头*/
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
----创建测试数据
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

----清除测试环境
drop function fnCheckEmail
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: