user818991
user818991

Reputation:

jQuery split text

I have a credit card field that populates a <span> tag e.g

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

I need to extract the date and find out if it is in the past.

At the moment I have the below jQuery but I'm stuck at the point of split() to extract just the date.

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

I think that covers everything but feel free to ask for more info

Upvotes: 0

Views: 374

Answers (3)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Try this:

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");

Upvotes: 1

filimonov
filimonov

Reputation: 1734

Small fix to Kolink's answer:

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");

(regexp doesn't need quotes)

Upvotes: 0

Jeff
Jeff

Reputation: 14279

I wouldnt split it. I would use a regex:

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

Upvotes: 0

Related Questions