2012-04-09 41 views
0

我已經繼承了以下腳本,需要對其進行改進。該功能檢查只輸入允許的字符(/),然後格式化輸入的日期,因爲1/1/12將重新格式化爲01/01/2012。這部分經過一些調整後效果很好。我現在需要進一步驗證並添加年份,如果它被忽略,這意味着如果用戶輸入1/1,則需要格式化並添加當前年份(例如01/01/2012)。的用戶輸入JS日期驗證增強

實施例和所要求(工作)輸出

  • A/A/A警報錯誤的用戶 - 檢查
  • 1/2/10更新輸入字段改爲01/03/2010
  • 12年1月1日更新輸入字段改爲01/01/2012
  • 1/10/2更新輸入字段改爲2002年1月10日

期望的更新(除以上) 1/9更新輸入字段讀取爲01/09/2012

這裏是當前函數(只要上述功能保留,歡迎您更改,重寫,無論如何)。 jQuery 1.7庫正在使用中,可以實現。

function ValidateDate(obj) 
{ 
/************************************************ 
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator has to be/
    Uses combination of regular expressions and 
    string parsing to validate date. 
    Ex. mm/dd/yyyy 

PARAMETERS: 
    ValidateDate(strValue) - String to be tested for validity 

RETURNS: 
    True if valid, otherwise false. 

REMARKS: 
    Avoids some of the limitations of the Date.parse() 
    method such as the date separator character. 
*************************************************/ 
    var checkOK = "/"; 
    var checkStr = obj.value; 
    var allValid = true; 
    var p = /(\d{1,2})\/(\d{1,2})\/(\d{1,4})/; 
    var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/; 
    // check to see if valid characters were used  
    for (i = 0; i < checkStr.length; i++) 
    { 
     ch = checkStr.charAt(i); 
     for (j = 0; j < checkOK.length; j++) 
      if (ch == checkOK.charAt(j)) 
      break; 
     if (j == checkOK.length) 
     { 
      allValid = false; 
      break; 
     } 
    } 
    if (!allValid) 
    { 
     alert("Please use only a combination of " + checkOK + "\'s charaters in the date field. Dates should be entered in the format of mm/dd/yyyy."); 
     setTimeout((function() { obj.select() }), 0); 
     return (false); 
    }  
    // converts to mm/dd/yyyy format 
    if (!obj.value.match(p)) return; 
    num=new Array(); 
    num=obj.value.match(p); 
    if (num[1].length == 1) num[1]="0" + num[1]; 
    if (num[2].length == 1) num[2]="0" + num[2]; 
    if (num[3].length == 1) num[3]="200" + num[3]; 
    if (num[3].length == 2) num[3]="20" + num[3]; 
    obj.value= num[1] + "/" + num[2] + "/" + num[3]; 
    //check to see if in correct format 
    if(!objRegExp.test(obj.value)) 
    { 
     alert('The date entered is not properly formatted.'); 
     return false; //doesn't match pattern, bad date 
    } 
    else{ 
     var arrayDate = obj.value.split(RegExp.$1); //split date into month, day, year 
     var intDay = parseInt(arrayDate[1],10); 
     var intYear = parseInt(arrayDate[2],10); 
     var intMonth = parseInt(arrayDate[0],10); 
    //check for valid month 
    if(intMonth > 12 || intMonth < 1) { 
     alert('The date entered is invalid'); 
     return false; 
    } 

    //create a lookup for months not equal to Feb. 
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31, 
         '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31} 

    //check if month value and day value agree 
    if(arrayLookup[arrayDate[0]] != null) { 
     if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0) 
     return true; //found in lookup table, good date 
    } 

    //check for February 
    var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0)); 
    if(((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0) 
     return true; //Feb. had valid number of days 
    } 
    alert(obj.value + ' is not a valid date.'); 
    // return false; //any other values, bad date 
} 
+0

那麼,這有什麼問題? – 2012-04-09 19:47:51

+0

@VladMinaev - 我如何修改這個腳本來產生所需的輸出。 * a/a/a錯誤提醒用戶 - 檢查 * 1/2/10更新輸入欄以讀取01/03/2010 * 01/01/12更新輸入欄以讀取爲01/01/2012 * 1/10/2更新輸入字段以讀取爲01/10/2002'只要它與此相同,就可以使用新腳本。 – HPWD 2012-04-09 20:32:02

回答

0

幾點反對:

  • 您沒有檢查有效字符,因爲你的對手正則表達式已經需要他們。如果你真的想,使用/^[\d\/]*$/.test()而不是那個循環。
  • 要匹配日期像1/1,使用/\d{1,2}\/\d{1,2}(\/\d{1,4})?/p,只是做了num = obj.value.split("/"),而不是匹配的組
  • 來驗證日期,看看javascript date validation using date object
  • 你也應該允許ISO日期格式YYYY-MM --DD,這是由本機解析Date()
+0

這些都不錯,但是正則表達式,我們看到字符沒有被捕獲。客戶希望她的團隊都使用相同的格式 - 尤其是因爲他們應該從其他來源複製日期並粘貼。他們的團隊負責人不希望他們在日期中輸入密碼以幫助減少輸入錯誤的機會(如果他們正在複製和粘貼,我們不會看到角色出現,但我會離題)。 – HPWD 2012-04-09 19:49:46

+0

呃,從另一個來源提取日期c&p web gui並不是我認爲的最佳解決方案... – Bergi 2012-04-09 19:52:22

+0

你對「沒有被捕獲的角色」有什麼意思? – Bergi 2012-04-09 19:53:25