tags:

views:

661

answers:

5

i have three drop down list for day month and year now i want to validate this selected date in asp.net using javascript or inbuild asp.net validation control.

thanks......

A: 

Can you at least post the code you have already so we can attempt to answer. There are several ways to validate dates in JS or in a code behind check but you need to provide more information.

Kaius
A: 

This is a java script code for validate date format :

<script language="javascript" type="text/javascript">
        function ValidateDate(args)
        {
        var date=args.Value;        
        var arr=date.split('/');

        if(arr.length!=3)
        {
        args.IsValid=false;
        return;
        }
        var day;
        if(arr[1]=='08')
        {
        day=parseInt('8');
        }
        else if(arr[1]=='09')
        {
        day=parseInt('9');
        }
        else
        {
            day=parseInt(arr[1]);
        }       
        var month;      
        if(arr[0]=='08')
        {
        month=parseInt('8');
        }
        else if(arr[0]=='09')
        {
        month=parseInt('9');
        }
        else
        {
            month=parseInt(arr[0]);
        }
        var year=parseInt(arr[2]);
        var boolday=false;
        var boolmonth=false;
        var boolyear=false;
        if(!isNaN(year))
        {   
         if(1800<year&&year<2100)
            {       

          boolyear=true;            
          }
        }   
        if(!isNaN(month))
        {       
            if(0<month&&month<13)
            {       

            boolmonth=true;
            }
        }
        if(!isNaN(day))
        {
        var val=32;
        if(boolmonth)
        {
             if(month==2)
             {
             if(boolyear)
             {
                if(year%4==0)
                {
                val=30;
                }
                else
                {
                val=29;
                }
             }

             }
             else if(month==4||month==6||month==11||month==9)
             {
             val=31
             }
        }

        if(0<day&&day<val)
        {   

        boolday=true;
        }
        }   


        if(boolyear&&boolmonth&&boolday)
        {

        args.IsValid=true;
        }
        else
        {
        args.IsValid=false;
        }

        }
        </script>

And you can validate date entered in the 3-DropDownList by concatinate the 3 values and passing it to the function

Ahmy
A: 

Using a custom validator:

protected void dobCustomValidator_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        CustomValidator validator = (CustomValidator) sender;

        ddlDateofBirthDay = (DropDownList)validator.Parent.FindControl("ddlDateofBirthDay");
        ddlDateofBirthMonth = (DropDownList)validator.Parent.FindControl("ddlDateofBirthMonth");
        ddlDateofBirthYear = (DropDownList)validator.Parent.FindControl("ddlDateofBirthYear");

        if (ddlDateofBirthDay.SelectedIndex == 0 || ddlDateofBirthMonth.SelectedIndex == 0 ||
            ddlDateofBirthYear.SelectedIndex == 0)
        {
            e.IsValid = false;
        }
        else
        {
            string dateOfBirthString = ddlDateofBirthDay.SelectedItem.Value + "/" + dateTools.MonthNumber(ddlDateofBirthMonth.SelectedItem.Value) +
                                       "/" + ddlDateofBirthYear.SelectedItem.Value;
            try
            {
                DateTime.Parse(dateOfBirthString, Culture);
            }
            catch
            {
                e.IsValid = false;
            }
        }
    }
A: 

See below. The main function is isDate to which you can pass day, month and year. These are javascript functions, so it alerts a relevant message and returns false.

The daysInFebruary will calculate the number of days for feb for the year passed.

The DaysArray keeps an array of the number of days for each month.

var minYear=1900;
var maxYear=2100;

function daysInFebruary (year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
        if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(strDay,strMonth,strYear ){
    var daysInMonth = DaysArray(12)

    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 (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }
    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)

    if (strMonth.length<1 || month<1 || month>12){
        alert("Please enter a valid month")
        return false
    }

    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        alert("Please enter a valid day")
        return false
    }

    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
        return false
    }


return true
}
Kamal
A: 

You could use javascript to load a hidden input with the value of the three dropdowns on the change event ie

hidDate.value = ddlDay.value + "/" + ddlMonth.value + "/" ddlYear.value;

Then use a compare validation control with the dataType set to DateTime validating the hidden input. You'll want to make sure that you the drop downs for values and only concat the /'s when needed.

(Note: This is for concept only and not going to be syntactically correct)

Shawn Hansen