views:

170

answers:

2

i am working on an already built program with a lot of classes and jscripts. the issue is the code has 3 dropdownlist's month, date, year. I need to remove the date one and passa static "01" value. here's the code for validating date in javascript -

        function Validate_date(sender, args) {
            var m = document.getElementById(sender.id.replace(/cv/, "m"));
            var d = document.getElementById(sender.id.replace(/cv/, "d"));
            var y = document.getElementById(sender.id.replace(/cv/, "y"));
            if (isDate(m.value, d.value, y.value)){
                var myDate = new Date();
                myDate.setFullYear(y.value, m.value - 1, d.value);
                var today = new Date();
                today.setDate(today.getDate()-30);
                args.IsValid = (myDate >= today);                
            }
            else{
                args.IsValid = false;
            }            
        } 

now the isdate function -

function isDate(strMonth, strDay, strYear) {
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1);
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1);
    for (var i = 1; i <= 3; i++) { if (strYear.charAt(0) == "0" && strYear.length > 1) strYear = strYear.substring(1); }
    month = parseInt(strMonth);
    day = parseInt(strDay);
    year = parseInt(strYear);
    if (strYear.length != 4 || year == 0) return false;
    if (strMonth.length < 1 || month < 1 || month > 12) return false;
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > DaysArray(12)[month]) return false;
    return true;
}

the problem is for dropdownlist when i give value= "01", it crashes on this line -

if (isDate(m.value, d.value, y.value)){ in the validate function

dropdownlist code -
<asp:DropDownList ID="EXP_d" runat="server" visible="false">
    <asp:ListItem Value="01">DD<ListItem>
<asp:DropDownList>

how can i pass the static value 01 from EXP_d (date) dropdownlist?

A: 

You can replace your isDate method with the following:

function isDate(strMonth, strDay, strYear) {
    var month = parseInt(strMonth, 10) - 1;
    var day = parseInt(strDay, 10);
    var year = parseInt(strYear, 10);

    var date = new Date(year, month, day);

    return date.getFullYear() === year
        && date.getMonth() === month
        && date.getDate() === day;
}
SLaks
A: 

Setting visible="false" on the control means ASP.Net will not render the control. Therefore, document.getElementById(sender.id.replace(/cv/, "d")) will return null. The dropdown control could be completely removed, but that has implications to the code behind. You could wrap the dropdown in a <div> with style='display:none' to hide it but allow the control to be available to javascript and server side code.

roryWoods