Validator.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="one.aspx.cs" Inherits="验证控件_one" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title> </title> <script language="javascript" type="text/javascript"> function checkNaN(source, args) { //alert(source + args); var obj = document.getElementById("TextBox6"); if (isNaN(obj.value)==true) { args.IsValid = false; } else { args.IsValid = true; } } </script> <style type="text/css"> .style1 { text-align: left; } .style2 { margin-left: 500px; } </style></head><body style="text-align: center"> <form id="form1" runat="server"> <div class="style1"> <p class="style2"> 账 名<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="账户为必填字段"></asp:RequiredFieldValidator> <br class="style2" /> 姓 名<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br class="style2" /> 密 码<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br class="style2" /> 确认密码<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox4" ControlToValidate="TextBox3" ErrorMessage="密码不一致"></asp:CompareValidator> <br class="style2" /> 年 龄<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox6" ErrorMessage="年龄必须是数字" ClientValidationFunction="CheckNaN"></asp:CustomValidator> <br class="style2" /> 电 话<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox5" ErrorMessage="电话格式不正确" ValidationExpression="(\(\d{3}\)|\d{3}-)?\d{8}"></asp:RegularExpressionValidator> <br class="style2" /> 生 日<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox> <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox7" ErrorMessage="生日必须介于1900年1月1日至2099年1月1日之间" MaximumValue="2099-1-1" MinimumValue="1900-1-1"></asp:RangeValidator> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="登录" /> <asp:Button ID="Button2" runat="server" Text="取消" /> </p> </div> </form></body></html>