Javascript日期验证不适用于Android设备4.4.4

时间:2014-12-01 14:04:49

标签: javascript android validation cordova

我正在开发手机间隙应用程序。在应用程序中我使用html文本框从用户输入日期,我使用javascript编写了日期验证代码。当我在模拟器上运行应用程序时,日期验证工作正常。但是当我在Android设备上运行应用程序时,日期验证无效。

设备:Micromax AQ4501 设备版本:Android Kitkat 4.4.4 模拟器:Android 4.4.2

代码:

var isShift=false;
var seperator = "/";
function DateFormat(txt , keyCode)
{

 if(keyCode==16)
     isShift = true;
 //Validate that its Numeric
 if(((keyCode >= 48 && keyCode <= 57) || keyCode == 8 ||
     keyCode <= 37 || keyCode <= 39 ||
     (keyCode >= 96 && keyCode <= 105)) && isShift == false)
 {
     if ((txt.value.length == 2 || txt.value.length==5) && keyCode !=   
     8)
    {
        txt.value += seperator;
    }
    return true;
 }
 else
 {
     return false;
 }
}

 function ValidateDate(txt,keyCode)
 {
 //alert("validate function called");
  if(keyCode==16)
     isShift = false;
 var val=txt.value;
 if(val.length == 10)
  {
    var splits = val.split("/");
    var dt = new Date(splits[1] + "/" + splits[0] + "/" + splits[2]);
    alert(dt);
    //Validation for Dates
     if(dt.getDate()==splits[0] && dt.getMonth()+1==splits[1]
               && dt.getFullYear()==splits[2])
        {
         //do nothing  
        }

      else
       {
          return;
       }

       //Range Validation
       if(txt.id.indexOf("txtRange") != -1)
          RangeValidation(dt);

       //BirthDate Validation
       if(txt.id.indexOf("txtBirthDate") != -1)               
        BirthDateValidation(dt)
       }
       else if(val.length < 10)      
       {             //do nothing
       }
       }

function BirthDateValidation(dt)
{
 var dtToday = new Date();
 var pastDate = new Date(Date.parse(dtToday.getMonth()+"/"+
    dtToday.getDate()+"/"+parseInt(dtToday.getFullYear()-100)));
 if (dt<pastDate || dt>=dtToday)
 {
   alert("Invalid BirthDate");
 }
 else
 {

     alert("Valid BirthDate");
  }
  }

 function RangeValidation(dt)
 {
 var startrange = new Date(Date.parse("01/01/1900"));
 var endrange = new Date(Date.parse("12/31/2099"));
 //var lblmesg = document.getElementById("<%=lblMesg.ClientID%>") ;
 if (dt<startrange || dt>endrange)
 {

     alert("Date should be between 01/01/1900 and 31/12/2099");
  }
  }

0 个答案:

没有答案