A 19-year-old man convicted of sleeping with an underage girl has been banned from having sex before marriage.

Cody Duane Scott Herrera, from the US state of Idaho, was given the unusual punishment after pleading guilty to the statutory rape of a 14-year-old girl, Associated Press reported.

Handed five-15 years in prison last week, Judge Randy Stoker suspended the sentence on condition Herrera carry out a one-year "rider" programme.

Stoker said on completion of the programme, the unmarried teen would be required to abstain from sex unless he weds.

The added terms of his sentence came after Herrera, from Twin Falls, had told investigators he's had 34 sexual partners.

"I have never seen that level of sexual activity by a 19-year-old," Stoker said. He told Herrera: "If you're ever on probation with this court, a condition of that will be you will not have sexual relations with anyone except who you're married to, if you're married."

But Shaakirrah Sanders, an associate professor at the University of Idaho College of Law, doubted the legality of the order.

She said while judges have discretion in creating special probation terms, the one applied to Herrera could be unenforceable and even illegal.

"I think it infringes on his constitutional rights," she said. "I think if he appealed, he would win."

She continued: "Probationers already give up a lot, especially in terms of law enforcement monitoring. But how would you even enforce this? It seems to be quite intrusive if they can just bust into his bedroom to make sure he's not getting it on."

Herrera pleaded guilty to the March 2015 statutory rape last year. The girl said when the two first met in 2014, she lied about her age, saying she was 16. The girl's mother found out about their relationship and contacted Herrera and his mother to inform them her daughter was only 14.

The girl told police she continued to chat with Herrera online, and that he sneaked into her bedroom through a window on 2 March. She then said she was sexually assaulted by Herrera.

"It was his intent from the beginning to take what he wanted from my 14-year-old child — her virginity," the victim's mother told the court, as reported by local news site Magic Valley.

"And he stayed around until he got it from her. Cody will never understand what he has done to our family. Cody robbed her of her innocence. He destroyed the child left in her. This can never be returned."