2011-05-30 84 views
0

我使用的CustomValidator验证日期,但这都不尽如人意任何一个可以告诉为什么ASP.NET的CustomValidator与ClientValidationFunction不点火,如果验证失败

这是我的.aspx

<script type="text/javascript"> 

function monthDiff(d1, d2) { 
    var months; 
    months = (d2.getFullYear() - d1.getFullYear()) * 12; 
    months -= d1.getMonth() + 1; 
    months += d2.getMonth(); 
    return months; 
} 

function difference(d1,d2){ 

var hiredate,dob; 
var diff=18*12; 
hiredate=document.getElementById(d1).value; 
dob=document.getElementById(d2).value; 
var months=monthDiff(hiredate,dob); 
if(diff<=months) 
{ 
return true; 
//true 
} 
else 
{ 
return false; 
//false 
} 
} 

function validatehiredate(value, arg) { 
       arg.IsValid = (difference('ctl00_ContentPlaceHolder1_txtHiredate','ctl00_ContentPlaceHolder1_txtDateofBirth')); 
      } 
</script> 

<asp:TextBox id="txtHiredate" runat="server" /> 
<asp:TextBox id="txtDateofBirth" runat="server" /> 
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validatehiredate" ControlToValidate="txtDateofBirth" ValidationGroup="personal" Display="Dynamic" ValidateEmptyText="True">can not hire guy less than 18 yrs</asp:CustomValidator> 

任何一个可以告诉一下这个

+0

到底是什么问题? – 2011-05-30 11:24:29

+0

如果我选择或提供相同的日期,它不会触发任何验证 – Dotnet 2011-05-30 11:25:49

+0

因此,它永远不会触发验证,或者只有当日期不一致时才会触发验证。 – 2011-05-30 11:27:49

回答

2

变化是错的你monthDiff功能,下面:

function monthDiff(d1, d2) { 
     var months; 
     var date1 = new Date(d1); 
     var date2 = new Date(d2); 

     months = (date2.getFullYear() - date1.getFullYear()) * 12; 
     months -= date1.getMonth() + 1; 
     months += date2.getMonth(); return months; 
     } 

对于除日期对象以外的任何内容,您都不能使用getFullYeargetMonth

我要做出你的浏览器是抛出一个JavaScript错误的假设,它只是没有弹出

编辑

 function getDays(d1, d2) { 
      var months; 
      var date1 = new Date(d1); 
      var date2 = new Date(d2); 
      return (date2 - date1)/(1000 * 60 * 60 * 24); 
      return months; 
     } 

     //function getLeapYear 

     function difference(d1, d2) 
     { 
      var hiredate, dob; var diff = 18 * 12; 
      hiredate = document.getElementById(d1).value; 
      dob = document.getElementById(d2).value; 
      var Age = getDays(hiredate, dob); 
      var compareVal = 365 * 18; //getCompareVal(hiredate,dob); 

      if (Age >= compareVal) { 
       return true; 
       //true 
      } else { 
       return false; //false 
      } 
     } 
+0

@ TBohnen.jnr:非常感谢 – Dotnet 2011-05-30 11:51:42

+0

很酷,很高兴它帮助 – 2011-05-30 11:53:21

+0

嘿,但如果我选择年龄大于18它也是射击 – Dotnet 2011-05-30 11:55:17