2012-09-01 42 views
-2

這裏是我的驗證不正確的時間格式代碼:的時間字段JavaScript驗證(0:00格式不被接受)

$('.allts').delegate(".tf", "focusout", function (e) 
     { 
       var curval = this.value; 
       curval = curval.replace(';', ':'); 
       curval = curval.replace(" ", ':'); 
       curval = curval.replace('.', ':');     
       //alert(curval); 
       if (/^\d$/.test(curval)) 
       { 
         curval = "0" + curval + ":00"; 
       } 
       if (/^[0-9]:[0-5][0-9]$/.test(curval)) 
         { 
           curval = "0" + curval; 
         } 
       if (curval >= 10 && curval <= 12) 
       { 
         curval = curval + ":00"; 
       } 
       if(curval.length==2&&curval >= 0 && curval <= 12){ 
       curval = curval + ":00"; 
       } 
       this.value = curval; 
       if (this.value != "") 
       { 
         if (!isValidDate(curval)) 
         { 
           this.style.background = "#faa"; 
           $('#msg_tformat').html('Please enter a valid time').show(); 
           this.value = ""; 
         } 

我的要求是不能接受的0:00時間格式。如果我給時間格式爲0:00,它應該顯示一個錯誤消息,如:「請輸入一個有效的時間」。任何人都可以建議 適當的方式來驗證這在JavaScript中嗎?

在此先感謝。

+0

這有什麼錯'如果(CURVAL === '0:00')...'? –

+0

感謝它現在的工作 – srikanth

回答

0

添加到isValidDate功能是這樣的:

if(curval === '0:00' || curval === '00:00') { 
    return false; 
} 
+0

感謝此代碼工作 – srikanth