0

I am validating date using customvalidator but this is not working well can any one tell why

This is my .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>

Can any one tell what's wrong in this

4
  • If i select or give the dates in equal it is not firing any validation Commented May 30, 2011 at 11:25
  • So it never fires off the validation, or it only fires off the validation when the dates are not the same? Commented May 30, 2011 at 11:27
  • Have you tried it without the validation group? Commented May 30, 2011 at 11:30
  • It is not firing for any ya i used validation group too Commented May 30, 2011 at 11:31

1 Answer 1

2

change your monthDiff function to the below:

 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;
       }

you can't use the getFullYear or getMonth on anything other than date objects.

I am going to make the assumption that you're browser is throwing a javascript error, it's just not popping up

EDIT

        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
            }
        }
Sign up to request clarification or add additional context in comments.

7 Comments

Hey but if i select age greater than 18 it is firing too
I think it is also necessary to change in year function too can u check once
TBohnen.jnr Hi dude are you there
yes sorry, I am looking into changing how you do it, at the moment you are only using the year as well as the month, If you use a more exact method it should be better, will update code now
ok, so change your monthDiff with my getdays and then also update the difference function. The only problem with this is you still need to subtract the leap years, which i will create a function for now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.