Labour was within its rights to stop new members voting in its leadership contest between Jeremy Corbyn and Owen Smith, the Court of Appeal has ruled.
It found Labour had the power "to set the criteria for members to be eligible to vote", effectively reintroducing the voting ban on nearly 130,000 members.
It overturns an earlier High Court ruling that the ban was unlawful.
A spokesman for leader Jeremy Corbyn's campaign said Friday's verdict was legally and democratically "wrong".
The contest between incumbent Mr Corbyn and challenger Mr Smith was sparked after Mr Corbyn lost a vote of no confidence by his MPs and faced mass resignations from his top team.
In July the National Executive Committee, the body that governs the Labour Party, ruled that full members could only vote if they had at least six months' continuous membership.
The 12 July "freeze date" was successfully challenged by some new members in the High Court.
But on Thursday, Labour appealed against the ruling, thought to benefit Mr Corbyn over Mr Smith.
Announcing the Court of Appeal's decision on Friday, Lord Justice Beatson said: "On the correct interpretation of the party rules, the National Executive Committee has the power to set the criteria for members to be eligible to vote in the leadership election in the way that it did."
The judgement said: "A member's entitlement to vote in a leadership election is not a product of him or her simply being a member, but is the result of him or her being a member who satisfies the precise eligibility criteria defined by the NEC and any freeze date provisions set by the NEC in the timetable for the election."
Labour's NEC, whose relationship with the party leadership has been under strain in recent months, had offered a window from 18-20 July when more recent members - and non-members - could pay £25 to become "registered supporters" and gain the right to vote.
Welcoming the court's ruling, NEC chairman Paddy Lillis said: "It is crucial to the Labour Party that our governing body has the authority to debate, decide and implement the procedures, timetable and voting eligibility for our internal elections and selections.
Analysis, BBC political correspondent Eleanor Garnier
The ruling by three Court of Appeal judges means around 130,000 new members who joined less than six months ago will now not be able to vote in the leadership contest.
It is thought the majority of the new joiners did so to back Jeremy Corbyn, so the fact they have, once again, been excluded from having their say is seen as a boost for his rival Owen Smith.
Mr Corbyn's campaign team said the judges' decision was wrong both legally and democratically.
He has always been the odds-on favourite so it is unclear how much of an impact the decision will have on the outcome of the leadership contest.
Once again, Labour is fighting its internal party battles in court in the full glare of the media, rather than behind closed doors. It is further evidence of the deep divisions entrenched in the party.
And this might not be the end of it - lawyers for the new members haven't ruled out a challenge in the highest court of the land.
"The original court decision had wide-ranging implications for the party and the authority of our governing body. It was the correct decision to seek clarification on this fundamental principle in the Court of Appeal."
But a spokesperson for Mr Corbyn's re-election campaign said: "We think that this is the wrong decision - both legally and democratically.
"The court's ruling disenfranchises nearly 130,000 Labour members who joined the party since January and were explicitly told that they would have a vote in any leadership election."
He said there were "serious questions" over "why and how the NEC Procedures Committee brought this appeal".
"In doing so, it effectively risked new members' money on an attempt to disenfranchise them. If we are to build a big, inclusive party to take on the Tories, we need to secure democracy in our party."
An application by the five party members, who brought the initial challenge to the party's rules, for permission to appeal to the Supreme Court was rejected.
But it is still open for them to apply directly to the highest court in the UK, if they want to do so.
Meanwhile, leadership challenger Owen Smith said the ruling "doesn't change my approach to this contest", saying he would continue to make his case as to why he should become Labour's new leader.
Reacting to the court's decision shadow chancellor John McDonnell said he was "flabbergasted" and "bitterly disappointed" for the members affected.
He attacked the 14 NEC members who agreed to apply a "retrospective" cut off date on voting rights in the first place and the decision to appeal against the High Court ruling.
"I think this is a grubby manoeuvre and people who have been involved in it should be ashamed of themselves," he told BBC Radio 5 Live.