利用ASP.NET的CustomValidator來檢查TextBox的日期格式
最近在MSDN論壇看到一個不錯的範例...
小弟就把它放到Blog...分享給大家呀...
ps.感謝提供此範例的chhuang大大
c#範例(VS2008)
CustomValidatorTest.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomValidatorTest.aspx.cs" Inherits="CustomValidatorTest" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head runat="server">
07 <title>CustomValidatorTest</title>
08 </head>
09 <body>
10 <form id="form1" runat="server">
11 <div>
12
13 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
14 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="送出" />
15 <asp:CustomValidator ID="CustomValidator1" runat="server"
16 ControlToValidate="TextBox1" ErrorMessage="CustomValidator"
17 onservervalidate="CustomValidator1_ServerValidate" ValidateEmptyText="True"></asp:CustomValidator>
18
19 </div>
20 </form>
21 </body>
22 </html>
23
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head runat="server">
07 <title>CustomValidatorTest</title>
08 </head>
09 <body>
10 <form id="form1" runat="server">
11 <div>
12
13 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
14 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="送出" />
15 <asp:CustomValidator ID="CustomValidator1" runat="server"
16 ControlToValidate="TextBox1" ErrorMessage="CustomValidator"
17 onservervalidate="CustomValidator1_ServerValidate" ValidateEmptyText="True"></asp:CustomValidator>
18
19 </div>
20 </form>
21 </body>
22 </html>
23
CustomValidatorTest.aspx.cs
01 using System;
02 using System.Collections;
03 using System.Configuration;
04 using System.Data;
05 using System.Linq;
06 using System.Web;
07 using System.Web.Security;
08 using System.Web.UI;
09 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 public partial class CustomValidatorTest : System.Web.UI.Page
15 {
16 protected void Page_Load(object sender, EventArgs e)
17 {
18
19 }
20 protected void Button1_Click(object sender, EventArgs e)
21 {
22 if (CustomValidator1.IsValid)
23 {
24 Response.Write("送出成功!");
25 }
26 }
27 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
28 {
29 DateTime dt;
30
31 if (string.IsNullOrEmpty(args.Value))
32 {
33 CustomValidator1.ErrorMessage = "該欄位必填";
34 args.IsValid = false;
35 }
36
37 else if (DateTime.TryParseExact(args.Value, "yyyy/MM/dd", null,System.Globalization.DateTimeStyles.AllowWhiteSpaces, out dt) == false)
38 {
39 CustomValidator1.ErrorMessage = "日期格式為 yyyy/MM/dd";
40 args.IsValid = false;
41 }
42 else
43 {
44 args.IsValid = true;
45 }
46 }
47 }
48
02 using System.Collections;
03 using System.Configuration;
04 using System.Data;
05 using System.Linq;
06 using System.Web;
07 using System.Web.Security;
08 using System.Web.UI;
09 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 public partial class CustomValidatorTest : System.Web.UI.Page
15 {
16 protected void Page_Load(object sender, EventArgs e)
17 {
18
19 }
20 protected void Button1_Click(object sender, EventArgs e)
21 {
22 if (CustomValidator1.IsValid)
23 {
24 Response.Write("送出成功!");
25 }
26 }
27 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
28 {
29 DateTime dt;
30
31 if (string.IsNullOrEmpty(args.Value))
32 {
33 CustomValidator1.ErrorMessage = "該欄位必填";
34 args.IsValid = false;
35 }
36
37 else if (DateTime.TryParseExact(args.Value, "yyyy/MM/dd", null,System.Globalization.DateTimeStyles.AllowWhiteSpaces, out dt) == false)
38 {
39 CustomValidator1.ErrorMessage = "日期格式為 yyyy/MM/dd";
40 args.IsValid = false;
41 }
42 else
43 {
44 args.IsValid = true;
45 }
46 }
47 }
48
執行結果:
參考網址:http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=3036812&SiteID=14