1

엔터티 프레임 워크.NET 4으로 사용하여 프로젝트를 개발 중입니다. 생년월일 필드가있는 가입 페이지가 있습니다. 나는 생년월일이 미래 또는 오늘이 될 수 없도록 검증하려고합니다. 게다가 그것은 오늘날과 비교하여 5 년 뒤가 될 수 있습니다.사용자 지정 유효성 검사기를 사용하여 미래 날짜 및 오늘 날짜를 방지하십시오.

여기 내 코드입니다. 페이지로드 여기

protected void Page_Load(object sender, EventArgs e) 
{ 
    Calendar1.EndDate = DateTime.Now; //to dissable future Date 
} 

Date of Birth: 
<asp:RequiredFieldValidator ID="rfv_txtbx_DOB" runat="server" ControlToValidate="txtbx_DOB" CssClass="validator" Display="Dynamic" ErrorMessage="Date of Birth Required" SetFocusOnError="True" Text="*" ValidationGroup="vg" /> 
<asp:CustomValidator ID="cv_txtbx_DOB" runat="server" ControlToValidate="txtbx_DOB" CssClass="validator" Display="Dynamic" 
    ErrorMessage="Date of Birth cannot be today or in future" SetFocusOnError="True" Text="*" ValidationGroup="vg" ClientValidationFunction="validateDate"/> 
<asp:TextBox ID="txtbx_DOB" runat="server" CssClass="txtbx" Width="200px" /> 
<ajaxToolkit:CalendarExtender ID="txtbx_DOB_CalendarExtender" runat="server" Enabled="True" Format="dd-MM-yyyy" TargetControlID="txtbx_DOB" /> 
<script language="javascript" type="text/javascript"> 
    function Validate(sender, args) { 
     var currentDate = new Date().getDate(); 

      if (args.Value < currentDate) 
       args.IsValid = true; 
      else 
       args.IsValid = false; 
     } 
</script> 
+0

다음은 uesful 링크입니다. http://stackoverflow.com/questions/6133539/how-can-i-validate-the-date-is-not-the-future-date-in-net-c-sharp – Ratna

답변

1

은 일정 연장에있는 모든 제한과 링크

http://www.karpach.com/ajaxtoolkit-calendar-extender-tweaks.htm

또는 다음 자바 스크립트 기능을 사용할 수 있습니다

function checkDate(sender, args) { 
    if (sender._selectedDate > new Date()) { 
      alert("You can select a day earlier than today!"); 
      sender._selectedDate = new Date(); 
      // set the date back to the current date 
      sender._textbox.set_Value(sender._selectedDate.format(sender._format)) 
    } 
+0

그 주위에 방법이 있지만 단순히 다른 것을 사용하지 않고 자바 스크립트 함수를 원하는 그래서 그것을 쉽게 다시 사용할 수 있습니다. 문제가있는 것은 완전한 날짜 (오늘 ddmmyyy로)를 얻고이를 내 컨트롤의 값과 비교하는 것입니다. 나는 젊은 사용자를 등록하는 것을 제한하고 싶다. 사용자는 16 세 이상이어야하며 자바 스크립트 나 jquery에별로 관심이 없습니다. 따라서이 문제를 해결하기 위해 필사적입니다. –

+0

당신은 클라이언트를 완전히 믿어서는 안됩니다. 자바 스크립트는 올바르지 않을 수도있는 사용자 시스템 날짜를 알려줍니다. 서버 측에서 설정하는 것이 좋습니다. –

+0

당신은 맞습니다.하지만 이것은 고객의 요구 사항입니다. 나는이 연령 constaraint 서버 측에서 쉽게 보장 할 수 있지만 클라이언트 측에 붙어있어 오류가 signup에 양식을 작성하는 동안 표시되어야합니다. –

3
function check_date() 
{ 
var chkdate = document.getElementById("ID OF YOUR TEXT FIELD HERE").value; 

var edate = chkdate.split("/"); 
var spdate = new Date(); 
var sdd = spdate.getDate(); 
var smm = spdate.getMonth(); 
var syyyy = spdate.getFullYear(); 
var today = new Date(syyyy,smm,sdd).getTime(); 
var e_date = new Date(edate[2],edate[1]-1,edate[0]).getTime(); 
if(e_date > today) 
{ 
    alert("Date is not valid"); 
    return false; 
} 
}