正则表达式的简单应用
一:JS(Jquery)中的正则
用户注册时,鼠标移开时自动验证用户输入的邮箱格式是否正确
1 //邮箱格式
2 $("#<%=txtEmail.ClientID %>").blur(function () {
3 var email = $("#<%=txtEmail.ClientID %>").val();
4 var regex = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
5 if (email != "") {
6 if (!regex.test(email)) {
7 alert("<%=Resources.global.revEmail %>");
8 $("#<%=txtEmail.ClientID %>").val("");
9 $("#<%=txtEmail.ClientID %>").focus();
10 return;
11 }
12 }
13 });
二:后台程序中
1.先必须引入命名空间
using System.Text.RegularExpressions;
2.if (friEmail != "" )
{
Match match = Regex.Match(friEmail, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (match.Success )//如果邮箱格式匹配成功
{
......执行代码
}