ASP.NET 自定义的一个日期验证控件

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.IO;
None.gif
using  System.Reflection;
None.gif
using  System.Web.UI;
None.gif
using  System.ComponentModel;
None.gif
None.gif
namespace  SmartWebControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 自定义的一个日期验证控件
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [ToolboxData("<{0}:DateVidatetor runat=\"server\"></{0}:DateVidatetor>")]
InBlock.gif    
public class DateVidatetor :BaseValidator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//服务器端double-check
InBlock.gif
        protected override bool EvaluateIsValid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string value = base.GetControlValidationValue(ControlToValidate);
InBlock.gif            DateTime dateValue;
InBlock.gif            
if (DateTime.TryParse(value, out dateValue))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"是否必须有值")]
InBlock.gif        
public bool MustHasValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ViewState["MustHasValue"== null ? false : (bool)ViewState["MustHasValue"];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ViewState["MustHasValue"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.AddAttributesToRender(writer);
InBlock.gif            
if (base.RenderUplevel)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//指定客户端验证函数
InBlock.gif
                writer.AddAttribute("evaluationfunction""DateValidatorIsValid");
InBlock.gif                
//注册自定义属性
InBlock.gif
                writer.AddAttribute("mustHasValue", MustHasValue ? "1" : "0");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnPreRender(e);
InBlock.gif            
//从编译的DLL中取出JavaScript脚本
InBlock.gif
            Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SmartWebControls.Common.js");
InBlock.gif            
string script;
InBlock.gif            
using (StreamReader sr = new StreamReader(sm))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                script 
= sr.ReadToEnd();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//注册客户端JavaScript脚本
InBlock.gif
            if (!Page.ClientScript.IsClientScriptBlockRegistered("SmartWebControls.Common.js"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "SmartWebControls.Common.js", script);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

Javascript文件:

 

None.gif < script language = " javascript " >
None.gif
<!--
None.gif
function  DateValidatorIsValid(val)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var value = ValidatorGetValue(val.controltovalidate);
InBlock.gif    
var mustHasValue = val.mustHasValue;
InBlock.gif    
InBlock.gif    
if( value == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//alert("Must input a value");
InBlock.gif
        if (mustHasValue =="1")
InBlock.gif            
return false;
InBlock.gif        
else
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
InBlock.gif    
return checkStringDate(value);
ExpandedBlockEnd.gif}

None.gif
None.gif
function  DateValidator2IsValid(val)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var value = ValidatorGetValue(val.txtID);    
InBlock.gif    
InBlock.gif    
if( value == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//alert("Must input a value");
InBlock.gif
        if (mustHasValue =="1")
InBlock.gif            
return false;
InBlock.gif        
else
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
InBlock.gif    
return checkStringDate(value);
ExpandedBlockEnd.gif}

None.gif
None.gif
function  checkStringDate(strDate)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var reg=/^(\ddot.gif{4})(\/)(\ddot.gif{2})(\/)(\ddot.gif{2})/;
InBlock.gif    
if(!reg.test(strDate))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        alert(
"日期格式不正确!\n正确格式为:2004-01-01");
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
var ss=strDate.split("/");
InBlock.gif    
var year=ss[0];
InBlock.gif    
InBlock.gif    
var monthValue=ss[1];    
InBlock.gif    
if(monthValue.toString().substring(0,1)== "0")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        monthValue 
= monthValue.toString().substring(1);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
var date=ss[2];
InBlock.gif    
if(date.toString().substring(0,1)== "0")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        date 
= date.toString().substring(1);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if(!checkYear(year))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{return false;}
InBlock.gif    
if(!checkMonth(monthValue))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{return false;}
InBlock.gif    
if(!checkDate(year,monthValue,date))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{return false;}
InBlock.gif    
return true;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
function  checkYear(year) dot.gif {
InBlock.gif    
if(isNaN(parseInt(year)))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        alert(
"年份输入有误,请重新输入!"); 
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if(parseInt(year)<1950 || parseInt(year) >2050)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        alert(
"年份应该在1950-2050之间!"); 
ExpandedSubBlockEnd.gif        
return false}

InBlock.gif    
else 
InBlock.gif        
return true;
ExpandedBlockEnd.gif}

None.gif
None.gif
function  checkMonth(monthValue)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
InBlock.gif    
if(isNaN(parseInt(monthValue)))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif        alert(
"月份输入有误,请重新输入!"); 
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if(parseInt(monthValue)<1 || parseInt(monthValue) >12)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        alert(
"月份应该在1-12之间! " ); 
InBlock.gif        
return false
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
InBlock.gif        
return true;
ExpandedBlockEnd.gif}

None.gif
None.gif
function  checkDate(year, monthValue, date)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {    
InBlock.gif    
var daysOfMonth=calcDays(parseInt(year),parseInt(monthValue));
InBlock.gif    
if(isNaN(parseInt(date)))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        alert(
"日期输入有误,请重新输入!"); 
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if(parseInt(date)<0||parseInt(date)>daysOfMonth)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        alert(
"日期应该在1-"+daysOfMonth+"之间!"); 
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else 
InBlock.gif        
return true;
ExpandedBlockEnd.gif}

None.gif
None.gif
function  calcDays(year, monthValue)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {    
InBlock.gif    
var date= new Date(year,monthValue,0);
InBlock.gif    
return date.getDate();
ExpandedBlockEnd.gif}

None.gif
None.gif
function  isLeapYear(year)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if( (year %4==0 && year %100!=0|| (year %400==0)) 
InBlock.gif        
return true;
InBlock.gif    
else
InBlock.gif        
return false;
ExpandedBlockEnd.gif}

None.gif
// -->
None.gif
</ script >
posted on 2007-06-02 12:17  阿牛-专注金融行业开发 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/rockniu/archive/2007/06/02/768583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值