使用java脚本进行时间验证

时间:2015-09-02 10:01:31

标签: javascript jquery

我尝试将我想要输入的内容存档为

  • 2 = 2小时
  • 15 = 15分钟
  • .5 = 30分钟
  • 2:45 = 2小时45分钟

所以我所做的或多或少是这样的,只是检查":"和24小时但我接缝这么无穷无尽的功能..

    function validateTime(obj)
{
    var timeValue = obj.value;
    if(timeValue == "" || timeValue.indexOf(":")<0)
    {
        alert("Invalid Time format");
        return false;
    }
    else
    {
        var sHours = timeValue.split(':')[0];
        var sMinutes = timeValue.split(':')[1];

        if(sHours == "" || isNaN(sHours) || parseInt(sHours)>23)
        {
            alert("Invalid Time format");
            return false;
        }
        else if(parseInt(sHours) == 0)
            sHours = "00";
        else if (sHours <10)
            sHours = "0"+sHours;

        if(sMinutes == "" || isNaN(sMinutes) || parseInt(sMinutes)>59)
        {
            alert("Invalid Time format");
            return false;
        }
        else if(parseInt(sMinutes) == 0)
            sMinutes = "00";
        else if (sMinutes <10)
            sMinutes = "0"+sMinutes;    

        obj.value = sHours + ":" + sMinutes;        
    }

    return true;    
    }

0 个答案:

没有答案