日期:2014-05-17  浏览次数:21234 次

C# C# 编写一个程序 判断用户输入的邮箱格式是否正确并提示 知道输入正确
C# 编写一个程序 判断用户输入的邮箱格式是否正确并提示 知道输入正确

------解决方案--------------------
用正则表达式判断:

 bool IsEmail(string emailStr)
{
return Regex.IsMatch(emailStr, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
------解决方案--------------------
bool IsValidEmail(string strEmail) 

return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 
}
------解决方案--------------------
这个验证,直接在前端,用JS验证,多方便啊?
JS验证用正则表达式,很方便
<script language="javascript" >
 function isText(test){
reg = new RegExp('^[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-z][a-z.]{2,8}$');
if(reg.test(test.value)){
//document.getElementById("emli").innerHTML="可以注册";
return true;
 
}else
{
return false;
}
 }
 function eMali(test){
if(isText.test(test)){
document.getElementById('emli').innerHTML="可以注册";
}
else
{
document.getElementById('emli').innerHTML="格式不正确";
}
 }
</script>