开发者

Can anyone tell me how to validate a date in dd/mm/yyyy format

开发者 https://www.devze.com 2023-03-05 09:08 出处:网络
Below is my code... /** * Check whether the values entered in the date fields are in dd/mm/yyyy format

Below is my code...

/**
* Check whether the values entered in the date fields are in dd/mm/yyyy format 
* and whether the dd, mm and yyyy values are valid
**/
function validateDateFormat(obj)
{
 var dtStr=obj.value;
 var year;
 var day;
 var month;
 var leap=0;
 var valid=true;
 var oth_valid=true;
 var feb=false;
 var validDate=true;
 var Ret=true;

  if(obj.readOnly==false)
  {
    if(dtStr!="" && dtStr!=null)
    {
        year=dtStr.substr(6,4);
        month=dtStr.substr(3,2);
        day=dtStr.substr(0,2);

        if(year=="0000" || year<1900 || month=="00" || day=="00" || dtStr.length<10)
开发者_运维知识库        {
            validDate=false;
        }

        if(validDate==true)
        {
            leap=year%4;

         if(month=="02")
         {
                feb=true;
         }


        if(leap==0 && month=="02")
            {
                if(day>29)
                {
                    valid=false;
                    feb=true;
                }
            }

            else if(month=="02" && day>28)
            {    
                valid=false;
                feb=true;
            }

            if(feb==false)
            {       
                if(month=="03" || month=="01" || month=="05" || month=="07" || month=="08" || month=="10" || month=="12")
                {
                    if(day>31)
                    {                       
                        oth_valid=false;
                    }
                }

                else if(month=="04" || month==06 || month=="09" || month=="11") 
                {
                    if(day>30)
                    {                       
                        oth_valid=false;
                    }
                }

                else
                {
                    oth_valid=false;
                }

            }
        }
    }   

     if(valid==false || oth_valid==false || validDate==false)
    {
        alert("Please enter the valid date in the dd/mm/yyyy Format only");
        obj.value="";
        obj.focus();
        Ret=false;
    }
    return Ret;
   }
}


You can use SimpleDateFormat to validate a date input string:

String input = "12/05/2011";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
try {
    Date date = sdf.parse(input);
    // it's good
} catch (ParseException e) {
    // it's bad
}


You can try to create a temporary date with the string, 
and check that the date and month match the input.
If you know that every user uses dd-mm-yyyy format a date can be made with
Date.parse(string)- but if you have any US users, better make the Date
with the constructor.

function isvalid_dmy(s){
    // ddmmyyyy date string
    var day, A= s.split(/\D+/);
    for(var i= 0; i<3; i++){
        A[i]= parseInt(A[i], 10)
    }
    A[1]-= 1;
    try{
        day= new Date(A[2], A[1], A[0]);
        if(day.getMonth()== A[1] && day.getDate()== A[0]){
            return day.toLocaleDateString();
            // or return true
        }
        return false;
    }
    catch(er){
        return false;// or handle bad date here 
    }
}


//test
var day1= '13-05-2011', day2= '31-04-2011';

day1+'= '+isvalid_dmy(day1)+'\n'+
day2+'= '+isvalid_dmy(day2);
/*  returned value: (String)
13-05-2011= Friday, May 13, 2011
31-04-2011= false
*/
0

精彩评论

暂无评论...
验证码 换一张
取 消