2012-07-26 49 views
0

我有填充一個<span>標籤的信用卡場e.gjQuery的分裂文本

<span>****-****-****-1111 (Expires 12/2012)</span> 

我需要提取的日期,並找出如果它是在過去。

此刻,我有以下的jQuery,但我被困在分割點()只提取日期。

var $selectedDate = $('.prev-card .chzn-container .chzn-single span').text().split(); 
var $now = new Date(); 
if ($selectedDate < $now) { 
    alert('past') 
} 
else{ 
    alert('future') 
} 

我認爲,涵蓋一切,但隨時要求獲得更多信息

+2

你爲什麼做這件事情與客戶端,而不是服務器側? – bugwheels94 2012-07-26 15:32:27

+0

@AnkitGautam也許這是爲了避免往返到服務器時,一些小事可以被檢測到的客戶端?我相信服務器端也有類似的驗證。 – 2012-07-26 15:33:30

+0

這是什麼.split()有用嗎? – Bergi 2012-07-26 15:42:27

回答

2

試試這個:

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/), 
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0), 
    now = new Date(); 
if(expires.getTime() < now.getTime()) alert("past"); 
else alert("future"); 
+0

遺漏的類型錯誤的整點:無法讀取屬性「2」空 – 2012-07-26 15:40:34

+0

的謝謝,固定@ user1555175錯誤修復 – 2012-07-26 15:45:07

0

我不會把它分解。我會使用一個正則表達式:

var value = $('.prev-card .chzn-container .chzn-single span').text(); 
/\d+\/\d+/.exec(value) //["12/2012"] 
1

小修復到Kolink的回答是:

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/), 
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0), 
    now = new Date(); 
if(expires.getTime() < now.getTime()) alert("past"); 
else alert("future"); 

(正則表達式不需要引號)

+0

謝謝,非常感謝 – 2012-07-26 15:45:38