<!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 id="Head1" runat="server">
<title>无标题页</title>
<script type ="text/ecmascript" language="javascript">
function validateDate(oSrc, args) {
var iDay, iMonth, iYear;
var arrValues;
arrValues = args.Value.split("-");
iYear = arrValues[0];
iMonth = arrValues[1];
iDay = arrValues[2];
var testDate = new Date(iYear, iMonth - 1, iDay);
if ((testDate.getDate() != iDay) ||
(testDate.getMonth() != iMonth - 1) ||
(testDate.getFullYear() != iYear)) {
args.IsValid = false;
return;
}
return true;
}
</script>
<style type="text/css">
.style1
{
font-size: 10.5pt;
}
#form1
{
width: 621px;
}
.style2
{
font-family: 宋体;
}
.sfl{ width:244px; text-align:left;float:left; text-align:left}
*{ font: normal 12px/160% Verdana, Lucida, Arial, Helvetica, 宋体,sans-serif;color:#666;}
</style>
</head>
<body>
<form id="form1" runat="server">
<span class="style1">
</span>
<br class="style1" />
<span class="style1">
<asp:Label ID="labelMessage" runat="server"></asp:Label>
</span>
<br class="style1" />
<br class="style1" />
<br class="style1" />
<span class="style1">
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click"
Text="测试区域性信息" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
</span>
<br />
<br />
*密码:
<asp:TextBox ID="TextBoxPassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBoxPassword" ErrorMessage="密码不能为空!"
Display="Dynamic">密码不能为空!</asp:RequiredFieldValidator>
<br />
<br />
*确认密码:<asp:TextBox ID="TextBoxPasswordConfirm" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBoxPasswordConfirm" ErrorMessage="密码不能为空!"
Display="Dynamic">密码不能为空!</asp:RequiredFieldValidator>
<br /><br />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBoxPassword" ControlToValidate="TextBoxPasswordConfirm"
ErrorMessage="密码不一致,重新输入。" Display="Dynamic">密码不一致,重新输入。</asp:CompareValidator>
<br />
<br />
出生日期: <asp:TextBox ID="TextBoxBirthday" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="TextBoxBirthday" ErrorMessage="正确格式如2008-8-25"
Operator="DataTypeCheck" Type="Date" Display="Dynamic">格式如2008-8-25</asp:CompareValidator>
<br />
<br />
输入未来的日期:<asp:TextBox ID="TextBoxFutureDate" runat="server" Height="20px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator3" runat="server"
ControlToValidate="TextBoxFutureDate" Display="Dynamic"
ErrorMessage="日期必须是今天以后的,格式如2008-8-25" Operator="GreaterThan" Type="Date"
ValueToCompare="2008-7-25">日期必须是今天以后的,格式如2008-8-25</asp:CompareValidator>
<br />
<br />
开始日期:<asp:TextBox ID="TextBoxStartDate" runat="server"></asp:TextBox>
<br />
<br />
结束日期:<asp:TextBox ID="TextBoxEndDate" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator4" runat="server"
ControlToCompare="TextBoxStartDate" ControlToValidate="TextBoxEndDate"
ErrorMessage="结束日期必须比开始日期大!" Operator="GreaterThan" Type="Date"
Display="Dynamic">结束日期必须比开始日期大!</asp:CompareValidator>
<br />
<br />
<br />
<span class="style2">像RangeValidator一样,如果不为要验证的表单字段输入值,<b
style="COLOR: black; BACKGROUND-COLOR: #ffff66">CompareValidator</b>也不会显示错误。如果想要求用户输入值,那么需要为该字段关联一个RequiredFieldValidator<b
style="COLOR: black; BACKGROUND-COLOR: #a0ffff">控件</b></span>。<br
class="style2" />
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Threading;
namespace ch
{
public partial class WebForm5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
labelMessage.Text = "";
}
protected void buttonSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
labelMessage.Text = "您的预订已处理!";
labelMessage.ForeColor = System.Drawing.Color.Red;
//Your reservation has been processed.
}
else
labelMessage.Text = "页面无效!";// Page is not valid.
}
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
try
{
DateTimeFormatInfo info1 = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.GetFormat(typeof(DateTimeFormatInfo));
//DateTimeFormatInfo 类:定义如何根据“区域性”设置DateTime值的格式并显示这些值。
//不同地区的日期表达格式不一样,有“年 月 日”、“月 日 年”等等。
DateTime time1 = DateTime.ParseExact(args.Value, "d", info1, DateTimeStyles.AllowWhiteSpaces);
//DateTime.ParseExact():使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTime。
//字符串表示形式的格式必须与指定的格式完全匹配。
//参数'd'表示“短日期模式”,表示由当前的 ShortDatePattern 属性定义的自定义日期和时间格式字符串。
//例如,由默认的当前区域性“zh-CN(中国)”的 ShortDatePattern 属性返回的自定义格式字符串为“yyyy-MM-dd”。
//Label1.Text = time1.ToLocalTime().ToString();
//Label1.Text += Label1.Text + Thread.CurrentThread.CurrentCulture.ThreeLetterWindowsLanguageName+Thread .CurrentThread .CurrentCulture .ThreeLetterISOLanguageName ;
//DateTime.ParseExact(args.Value, "d", null);
args.IsValid = true;
}
catch
{
args.IsValid = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string dt = "20061230";
//CultureInfo cinfo =Thread.CurrentThread.CurrentCulture.NativeName;
DateTimeFormatInfo info1 = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.GetFormat(typeof(DateTimeFormatInfo));
DateTime time1 = DateTime.ParseExact(dt, "yyyyMMdd", info1, DateTimeStyles.AllowWhiteSpaces);
Label1.Text = time1.ToLocalTime().ToString() + "<br/> " + info1.ToString() + "<br/>" + info1.FullDateTimePattern + "<br/>" + info1.ShortDatePattern + "<br/>" + Page.Culture.ToString();
// Display using zh-CN culture's short date format
DateTime thisDate = new DateTime(2008, 3, 15);
// Display using zh-CN culture's short date format
CultureInfo culture = new CultureInfo("zh-CN");
Label1.Text += "<br/>" + thisDate.ToString("d", culture) + "<br/>"; // Displays 15/3/2008
//CultureInfo类:提供有关特定区域性的信息(对于非托管代码开发,则称为“区域设置”)。
//这些信息包括区域性的名称、书写系统、使用的日历以及对日期和排序字符串的格式化设置。
DateTimeFormatInfo fmt = (new CultureInfo("zh-CN")).DateTimeFormat;
Label1.Text += thisDate.ToString("d", fmt); // Displays 2008-3-15
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
}
}