会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
ZZ2
小胜凭智 大胜靠德
博客园
首页
新随笔
新文章
联系
订阅
管理
C#检测输入的邮件地址是否合法
/**/
///
<summary>
///
检测输入的邮件地址strEmail是否合法,非法则返回true。
///
</summary>
public
bool
CheckEmail(
string
strEmail)
{
int
i,j;
string
strTmp,strResult;
string
strWords
=
"
abcdefghijklmnopqrstuvwxyz_-.0123456789
"
;
//
定义合法字符范围
bool
blResult
=
false
;
strTmp
=
strEmail.Trim();
//
检测输入字符串是否为空,不为空时才执行代码。
if
(
!
(strTmp
==
null
||
strTmp.Length
==
0
))
{
//
判断邮件地址中是否存在“@”号
if
((strTmp.IndexOf(
"
@
"
)
<
0
))
{
blResult
=
true
;
return
blResult;
}
//
以“@”号为分割符,把地址切分成两部分,分别进行验证。
string
[] strChars
=
strTmp.Split(
new
char
[]
{
'
@
'
}
);
foreach
(
string
strChar
in
strChars)
{
i
=
strChar.Length;
//
“@”号前部分或后部分为空时。
if
(i
==
0
)
{
blResult
=
true
;
return
blResult;
}
//
逐个字进行验证,如果超出所定义的字符范围strWords,则表示地址非法。
for
(j
=
0
;j
<
i;j
++
)
{
strResult
=
strChar.Substring(j,
1
).ToLower();
if
(strWords.IndexOf(strResult)
<
0
)
{
blResult
=
true
;
return
blResult;
}
}
}
}
return
blResult;
}
posted on
2005-07-31 12:58
Judy
阅读(
989
) 评论(
1
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部