我建議您使用客戶端JavaScript函數asp:CustomValidator
。
Custom Validator
你可以做這樣的事情(注意,這是未經測試,只是把我的頭頂部):
<asp:CustomValidator ID="CustomValidator1" runat="server"
EnableClientScript="true"
ErrorMessage="less than 18"
ClientValidationFunction="checkDate"
ControlToValidate="txtDOB">
</asp:CustomValidator>
假設日期是寫在這個格式「DD/MM/YYYY」
function checkDate() {
var enteredDate=document.getElementById('<%=txtDOB.ClientID%>').value;
var dateValues=enteredDate.split("/");
var dateToCheck = new Date(dateValues[2], dateValues[1]-1, dateValues[0]);
var today = new Date();
var dateValid = new Date(today.getFullYear()-18, today.getMonth()-1, today.getDate());
if (dateToCheck < dateValid) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
注意
- javascript對1月使用0,因此該月使用-1。
- 如果javascript被禁用,您應該將ServerValidation函數添加到自定義驗證器。
訪問http://stackoverflow.com/questions/939802/date-validation-with-asp-net-validator?rq=1 – Satpal