主题:  ASP.NET学习手记:验证用户表单输入(转载)

design_5d

职务:普通成员
等级:2
金币:1.0
发贴:252
#12001/10/30 15:29:24
ASP.NET学习手记:验证用户表单输入
(转载 动网先锋)


运行环境:Win2000 Advance Server+IIS5.0+NGWS SDK(80M的).
其中验证email地址的函数用的是廖兄精华区中的代码.
<%@ Page Language="C#" %>

<html>
<script language="javascript">
function ClientValidateEmail(source, value)
{
var strr;
re=/(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
re.exec(value);
if (RegExp.$3!=""&&RegExp.$3!="."&&RegExp.$2!=".") strr=RegExp.$1+RegExp.$2+RegExp.$3
else
if (RegExp.$2!=""&&RegExp.$2!=".") strr=RegExp.$1+RegExp.$2
else strr=RegExp.$1
if (strr!=value)
return false;
else
return true;
}
</script>
<body>

ASP.NET验证用户输入



<form method=post runat=server>
<hr width=600 size=1 noshade>
<table>
<tr>
<td>
<asp:ValidationSummary ID="valSum" runat="server"
HeaderText="你必须输入下面这些表单域:"
DisplayMode="bulletlist"
Font-Name="宋体"
Font-Size="12"
/>
</td>
</tr>
</table>




<!-- 登陆信息 -->
<table border=0 width=600 >
<tr><td colspan=3>
<table border=0 cellpadding=0 cellspacing=0 width="100%">
<tr><td>
登陆信息
</td></tr>
</table>
</td></tr>
<tr>
<td align=right>
Email地址:
</td>
<td>
<asp:TextBox id=email width=200px maxlength=60 runat=server />
</td>
<td>
<asp:RequiredFieldValidator id="emailReqVal"
ControlToValidate="email"
ErrorMessage="Email地址"
Display="Dynamic"
Font-Name="宋体"
Font-Size="12"
runat=server>
*
</asp:RequiredFieldValidator>
<asp:CustomValidator id="emailRegexVal" runat="server"
ControlToValidate="email"
ClientValidationFunction="ClientValidateEmail"

Display="Static"
Font-Name="宋体"
Font-Size="12">
非法Email地址
</asp:CustomValidator>
</td>
</tr>

<tr>
<td align=right>
密码:
</td>
<td>
<asp:TextBox id=passwd TextMode="Password" maxlength=20 runat=server/>
</td>
<td>
<asp:RequiredFieldValidator id="passwdReqVal"
ControlToValidate="passwd"
ErrorMessage="用户密码"
Display="Dynamic"
Font-Name="宋体" Font-Size="12"
runat=server>
*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="passwdRegexBal"
ControlToValidate="passwd"
ValidationExpression=".*[!@#$%^&*+;:].*"
Display="Static"
Font-Name="宋体" Font-Size="12"
Width="100%" runat=server>
密码必须包含如下字符: (!@#$%^&*+;:)
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td align=right>
确认密码
</td>
<td>
<asp:TextBox id=passwd2 TextMode="Password" maxlength=20 runat=server/>
</td>
<td>
<asp:RequiredFieldValidator id="passwd2ReqVal"
ControlToValidate="passwd2"
ErrorMessage="确认密码"
Display="Dynamic"
Font-Name="宋体" Font-Size="12"
runat=server>
*
</asp:RequiredFieldValidator>
<asp:CompareValidator id="CompareValidator1"
ControlToValidate="passwd2" ControlToCompare="passwd"
Display="Static"
Font-Name="宋体" Font-Size="12"
runat=server>
密码不符合
</asp:CompareValidator>
</td>
</tr>
<tr><td colspan=3> </td></tr>


<!-- 私人信息 -->
<tr><td colspan=3>
<table border=0 cellpadding=0 cellspacing=0 width="100%">
<tr><td>
个人信息
</td></tr>
</table>
</td></tr>
<tr>
<td align=right>
姓名:
</td>
<td>
<asp:TextBox id=pname maxlength=20 width=200px runat=server />
</td>
<td>
</td>
</tr>
<tr>
<td align=right>
地址:
</td>
<td>
<asp:TextBox id=address width=200px runat=server />
</td>
<td>
</td>
</tr>
<tr>
<td align=right>
邮编:
</td>
<td>
<ASP:TextBox id=postcode width=60px maxlength=6 runat=server />
</td>
<td>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="postcode"
ValidationExpression="^\d{6}$"
Display="Static"
Font-Name="宋体" Font-Size="12"
runat=server>
邮编必须是6位数字
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td align=right>
性别:
</td>
<td>
<ASP:RadioButtonList id=sexType runat=server>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</ASP:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator id="sexTypeReqVal"
ControlToValidate="sexType"
ErrorMessage="性别"
Display="Static"
Initialvalue=""
Font-Name="宋体" Font-Size="12"
runat=server>
*
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align=right>
出生年份:
</td>
<td>
<ASP:DropDownList id=expYear runat=server>
<asp:ListItem></asp:ListItem>
<asp:ListItem >1976</asp:ListItem>
<asp:ListItem >1975</asp:ListItem>
</ASP:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator id="expDateReqVal"
ControlToValidate="expYear"
ErrorMessage="出生年份"
Display="Static"
Initialvalue=""
Font-Name="宋体" Font-Size="12"
runat=server>
*
</asp:RequiredFieldValidator>
</td>
</tr>
</table>



<input runat="server" type=submit value="提 交">


</form>

</body>
</html>




编辑历史:[此帖最近一次被 allinhands 编辑过(编辑时间:2006-02-27 09:27:25)]

别和我提网页设计,一天到晚就和这东西打交道,小心,看爆了眼睛[img]http://www.deskcity.com/img/forum/face/xl/032.gif[/img]

mll0209

职务:普通成员
等级:1
金币:0.0
发贴:8
#22006/2/27 9:17:38
呵呵