Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dears ,

I need to pass the selected item from a DropDownList to a Javascript function using OnClientClick to validate it when the botton is cliked on the cilent side.

here is my code that does not work with me , can any body tell me where is the issue:

XML
<asp:Button ID="Save" runat="server" Text="Save" Width="173px"
                        onclick="_Run_Click" OnClientClick="return DateValidation('<%=yFrom.ClientID%>','<%=yTo.ClientID%>')"  />



XML
<script type="text/javascript">
    function DateValidation(yFrom, yTo) {

        var startYear = document.getElementById(yFrom);
        var endYear = document.getElementById(yTo);
        if (startYear != '' && endYear != '')
         {
             if (endYear < startYear)
            {
                alert('Start date cannot be greater than end date');
                 return false;
             }
            else { return true; }
         }
     }
</script>


Thanks,
Posted

1 solution

JavaScript
var startYear = document.getElementById(yFrom);

This gave you just the control object. Sounds like you want to compare the two date value.

Try:
JavaScript
function DateValidation(yFrom, yTo) {

       var startYear = document.getElementById(yFrom);
       var endYear = document.getElementById(yTo);
       if (startYear.value != '' && endYear.value != '')
        {
            if (endYear.value < startYear.value)
           {
               alert('Start date cannot be greater than end date');
                return false;
            }
           else { return true; }
        }
    }


UPDATE:
For date comparison, you need to convert the textbox strings into dates.

Here:
JavaScript
function DateValidation(yFrom, yTo) {

       var startYear = document.getElementById(yFrom);
       var endYear = document.getElementById(yTo);
       if (startYear.value != '' && endYear.value != '')
        {
            Date endDate = new Date(endYear.value);
            Date startDate = new Date(startYear.value);
            if (endDate < startDate)
           {
               alert('Start date cannot be greater than end date');
                return false;
            }
           else { return true; }
        }
    }

Make sure that the textbox has a valid Javascript date or else you will get an error.
 
Share this answer
 
v3
Comments
AJ Hoge 26-Apr-12 16:24pm    
i have the following error meessage when i use ".value"
"Microsoft JScript runtime error: Object required"
any advice ?
Sandeep Mewara 26-Apr-12 22:39pm    
Do a quickwatch and see if endYear or startYear is correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900