using
System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using System.Reflection;
using System.Web.UI;
using System.ComponentModel;
namespace SmartWebControls
{
/**//// <summary>
/// 自定义的一个日期验证控件
/// </summary>
[ToolboxData("<{0}:DateVidatetor runat=\"server\"></{0}:DateVidatetor>")]
public class DateVidatetor :BaseValidator
{
//服务器端double-check
protected override bool EvaluateIsValid()
{
string value = base.GetControlValidationValue(ControlToValidate);
DateTime dateValue;
if (DateTime.TryParse(value, out dateValue))
{
return true;
}
else
{
return false;
}
}
[Description("是否必须有值")]
public bool MustHasValue
{
get
{
return ViewState["MustHasValue"] == null ? false : (bool)ViewState["MustHasValue"];
}
set { ViewState["MustHasValue"] = value; }
}
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (base.RenderUplevel)
{
//指定客户端验证函数
writer.AddAttribute("evaluationfunction", "DateValidatorIsValid");
//注册自定义属性
writer.AddAttribute("mustHasValue", MustHasValue ? "1" : "0");
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//从编译的DLL中取出JavaScript脚本
Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SmartWebControls.Common.js");
string script;
using (StreamReader sr = new StreamReader(sm))
{
script = sr.ReadToEnd();
}
//注册客户端JavaScript脚本
if (!Page.ClientScript.IsClientScriptBlockRegistered("SmartWebControls.Common.js"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SmartWebControls.Common.js", script);
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using System.Reflection;
using System.Web.UI;
using System.ComponentModel;
namespace SmartWebControls
{
/**//// <summary>
/// 自定义的一个日期验证控件
/// </summary>
[ToolboxData("<{0}:DateVidatetor runat=\"server\"></{0}:DateVidatetor>")]
public class DateVidatetor :BaseValidator
{
//服务器端double-check
protected override bool EvaluateIsValid()
{
string value = base.GetControlValidationValue(ControlToValidate);
DateTime dateValue;
if (DateTime.TryParse(value, out dateValue))
{
return true;
}
else
{
return false;
}
}
[Description("是否必须有值")]
public bool MustHasValue
{
get
{
return ViewState["MustHasValue"] == null ? false : (bool)ViewState["MustHasValue"];
}
set { ViewState["MustHasValue"] = value; }
}
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (base.RenderUplevel)
{
//指定客户端验证函数
writer.AddAttribute("evaluationfunction", "DateValidatorIsValid");
//注册自定义属性
writer.AddAttribute("mustHasValue", MustHasValue ? "1" : "0");
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//从编译的DLL中取出JavaScript脚本
Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SmartWebControls.Common.js");
string script;
using (StreamReader sr = new StreamReader(sm))
{
script = sr.ReadToEnd();
}
//注册客户端JavaScript脚本
if (!Page.ClientScript.IsClientScriptBlockRegistered("SmartWebControls.Common.js"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SmartWebControls.Common.js", script);
}
}
}
}
Javascript文件:
<
script language
=
"
javascript
"
>
<!--
function DateValidatorIsValid(val)
{
var value = ValidatorGetValue(val.controltovalidate);
var mustHasValue = val.mustHasValue;
if( value == "")
{
//alert("Must input a value");
if (mustHasValue =="1")
return false;
else
return true;
}
return checkStringDate(value);
}
function DateValidator2IsValid(val)
{
var value = ValidatorGetValue(val.txtID);
if( value == "")
{
//alert("Must input a value");
if (mustHasValue =="1")
return false;
else
return true;
}
return checkStringDate(value);
}
function checkStringDate(strDate)
{
var reg=/^(\d{4})(\/)(\d{2})(\/)(\d{2})/;
if(!reg.test(strDate))
{
alert("日期格式不正确!\n正确格式为:2004-01-01");
return false;
}
var ss=strDate.split("/");
var year=ss[0];
var monthValue=ss[1];
if(monthValue.toString().substring(0,1)== "0")
{
monthValue = monthValue.toString().substring(1);
}
var date=ss[2];
if(date.toString().substring(0,1)== "0")
{
date = date.toString().substring(1);
}
if(!checkYear(year))
{return false;}
if(!checkMonth(monthValue))
{return false;}
if(!checkDate(year,monthValue,date))
{return false;}
return true;
}
function checkYear(year) {
if(isNaN(parseInt(year)))
{
alert("年份输入有误,请重新输入!");
return false;
}
else if(parseInt(year)<1950 || parseInt(year) >2050)
{
alert("年份应该在1950-2050之间!");
return false}
else
return true;
}
function checkMonth(monthValue)
{
if(isNaN(parseInt(monthValue)))
{
alert("月份输入有误,请重新输入!");
return false;
}
else if(parseInt(monthValue)<1 || parseInt(monthValue) >12)
{
alert("月份应该在1-12之间! " );
return false
}
else
return true;
}
function checkDate(year, monthValue, date)
{
var daysOfMonth=calcDays(parseInt(year),parseInt(monthValue));
if(isNaN(parseInt(date)))
{
alert("日期输入有误,请重新输入!");
return false;
}
else if(parseInt(date)<0||parseInt(date)>daysOfMonth)
{
alert("日期应该在1-"+daysOfMonth+"之间!");
return false;
}
else
return true;
}
function calcDays(year, monthValue)
{
var date= new Date(year,monthValue,0);
return date.getDate();
}
function isLeapYear(year)
{
if( (year %4==0 && year %100!=0) || (year %400==0))
return true;
else
return false;
}
// -->
</ script >
<!--
function DateValidatorIsValid(val)
{
var value = ValidatorGetValue(val.controltovalidate);
var mustHasValue = val.mustHasValue;
if( value == "")
{
//alert("Must input a value");
if (mustHasValue =="1")
return false;
else
return true;
}
return checkStringDate(value);
}
function DateValidator2IsValid(val)
{
var value = ValidatorGetValue(val.txtID);
if( value == "")
{
//alert("Must input a value");
if (mustHasValue =="1")
return false;
else
return true;
}
return checkStringDate(value);
}
function checkStringDate(strDate)
{
var reg=/^(\d{4})(\/)(\d{2})(\/)(\d{2})/;
if(!reg.test(strDate))
{
alert("日期格式不正确!\n正确格式为:2004-01-01");
return false;
}
var ss=strDate.split("/");
var year=ss[0];
var monthValue=ss[1];
if(monthValue.toString().substring(0,1)== "0")
{
monthValue = monthValue.toString().substring(1);
}
var date=ss[2];
if(date.toString().substring(0,1)== "0")
{
date = date.toString().substring(1);
}
if(!checkYear(year))
{return false;}
if(!checkMonth(monthValue))
{return false;}
if(!checkDate(year,monthValue,date))
{return false;}
return true;
}
function checkYear(year) {
if(isNaN(parseInt(year)))
{
alert("年份输入有误,请重新输入!");
return false;
}
else if(parseInt(year)<1950 || parseInt(year) >2050)
{
alert("年份应该在1950-2050之间!");
return false}
else
return true;
}
function checkMonth(monthValue)
{
if(isNaN(parseInt(monthValue)))
{
alert("月份输入有误,请重新输入!");
return false;
}
else if(parseInt(monthValue)<1 || parseInt(monthValue) >12)
{
alert("月份应该在1-12之间! " );
return false
}
else
return true;
}
function checkDate(year, monthValue, date)
{
var daysOfMonth=calcDays(parseInt(year),parseInt(monthValue));
if(isNaN(parseInt(date)))
{
alert("日期输入有误,请重新输入!");
return false;
}
else if(parseInt(date)<0||parseInt(date)>daysOfMonth)
{
alert("日期应该在1-"+daysOfMonth+"之间!");
return false;
}
else
return true;
}
function calcDays(year, monthValue)
{
var date= new Date(year,monthValue,0);
return date.getDate();
}
function isLeapYear(year)
{
if( (year %4==0 && year %100!=0) || (year %400==0))
return true;
else
return false;
}
// -->
</ script >