"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Wednesday, March 2, 2011

javascript dropdown date validation

Hi,
i have a grt hour on last day.my client don't need any date picker then second option of my is a drop-down.he accept that but validation showed be done on client side!!!!.
problem to face are :
1.Make sure the month selected has only 30 days
2. vice-verse also 31 days
3. Feb had only 28 day's
4. check year is leap year if so 29 day's.
Then i plan and create a JavaScript that validate date as per criteria 
Demo

anyway here is the javascript


    <script type="text/javascript">
        function chkDate(d, m, y) {
            var td = 0;
            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
                td = 31;
            else if (m == 4 || m == 6 || m == 9 || m == 11)
                td = 30;
            else {
                if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) td = 29;
                else td = 28;
            }
            if (td >= d) return true;
            else return false;
        }
        function validate() {
          //  debugger;
            if (chkDate(document.getElementById('dd').value, document.getElementById('mm').value, document.getElementById('yy').value) == false) {
                alert("Invalid Date");
                return false;
            }
            else {
                alert("Valid Date");
            }
        }
      
    </script>


stay tune....

Have a nice day... 'N happy Coding :)

1 comment:

Randheer Kumar said...

thank you for this article its really plain and simple to understand