WASHINGTON—By the time President Trump took the stage in Missouri to address the Veterans of Foreign Wars in late July, his speech had already gone through the usual wringer with officials at both the White House and Department of Veterans Affairs.

They had negotiated with Stephen Miller, Mr. Trump’s top adviser and speech writer, and his team to purge it of any problematic partisan references since the VA was funding the trip, according to two people familiar with the process.

Their aim was to avoid violating the Hatch Act, which stipulates that government officials mustn’t engage in political activity in an official capacity at any time. The president is one of very few exceptions to that rule. The VA is not.

When in Kansas City, Mr. Trump opened his prepared remarks by paying tribute to the men and women who “defended our nation in battle and protected our great American flag.”

Seven minutes in, he abandoned his script. “We need Josh badly,” he said of Missouri Republican Attorney General Josh Hawley, a candidate for the Senate this fall, calling him up on the stage.

“Come up here, just shake my hand,” Mr. Trump said, before giving Mr. Hawley the mic.

Mr. Hawley hailed Mr. Trump’s “courage” and “leadership,” then closed his brief remarks by adding a pitch for votes: “I think he needs reinforcements in Washington.”

A complaint last week by Citizens for Responsibility and Ethics in Washington, a watchdog organization, warned that “several official government events in which President Trump, members of his cabinet, and other administration officials participated are part of the president’s efforts to help Republican candidates in the coming midterm elections.”

The complaint, submitted to White House Chief of Staff John Kelly and Henry Kerner at the Office of Special Counsel, added that “these actions appear to be directed toward the success or failure of candidates in partisan races and thus may constitute political activity prohibited by the Hatch Act, and the costs of attending them must be properly apportioned.”

The WSJ's Gerald F. Seib talks about the numbers that are important to determine control of the House and Senate. Photo: Getty

Mr. Trump’s tendency to go off script has already sent some government agencies legal offices researching their potential exposure to Hatch Act allegations that ultimately don’t touch Mr. Trump himself but could affect them.

Penalties can range from an official reprimand to a civil penalty of up to $1,000. More serious infringements can risk suspension, termination or a ban of up to five years from federal employment for individuals.

The White House asserts “there is no legal prohibition” on the Mr. Trump endorsing candidates at taxpayer-funded events, said spokesman Hogan Gidley, noting that “the Hatch Act does not apply to the president or the vice president.”

Former President Barack Obama’s secretary of Housing and Urban Development, Julian Castro, was cited for a Hatch Act violation when he responded to journalist Katie Couric’s questions about the 2016 presidential race and his own chances of being chosen as Democratic candidate Hillary Clinton’s running mate.

“Historically, this type of issue is focused on costs surrounding an event—primarily traveling expenses of the president,” said Jan Witold Baran, an election law and government ethics attorney with Wiley Rein. In the case of Mr. Trump’s speeches, “there are potential violations, at the very least, of appropriations laws.”

In Iowa, at a July event dubbed “Pledge to American Workers,” Mr. Trump hailed Republican Rep. Rod Blum, saying, “Without Rod, we wouldn’t have our tax cuts, and we have massive tax cuts.” A number of senior government employees participated in the event, including Labor Secretary Alexander Acosta and Mr. Trump’s senior adviser, his daughter Ivanka Trump.

Days later in Illinois, the line was once again blurred when Mr. Trump used an official event to highlight a recently reopened steel plant. “You’ve got to vote Republican, folks, you’ve got to vote Republican,” Mr. Trump said at the steel plant in Granite City, Ill. “Vote for these two congressmen; they know what we’re doing. They know what they’re doing. They’re tough, and they’re smart.”

Jordan Libowitz, a spokesman for CREW, said “these events should be thoroughly investigated, and this activity must immediately stop. Any improperly used taxpayer funds must be reimbursed.”

The Office of Special Counsel—not affiliated with the Justice Department’s special counsel Russia investigation led by Robert Mueller—is a government body charged with enforcing the Hatch Act, a law passed in 1939 that aims to prevent the government from influencing elections or acting in a partisan manner.

An official with the office said issuing a violation of this nature requires information about why the political candidates are attending the event and whether the government agency knew in advance that the candidates would be there.

This becomes tricky when dealing with incumbent officials, since they may be attending official events in their current government capacity.

There have been more straightforward violations for comments made by officials with the Trump administration.

Since Mr. Trump took office, White House aide Kellyanne Conway has been reprimanded twice—once for advocating for Republican Roy Moore in Alabama’s special election for Senate in a television interview while appearing in her official capacity, and a second time for plugging Ivanka Trump’s clothing line, violating a similar Office of Government Ethics prohibition on product endorsements for private gains.

White House social media director Dan Scavino and Ambassador to the United Nations Nikki Haley have also received official warnings over tweets that the OSC said broke the rules.

Corrections & Amplifications

White House aide Kellyanne Conway has been reprimanded for two violations—one through the Office of Special Counsel, and the other through the Office of Government Ethics. An earlier version of this article incorrectly stated that Ms. Conway has been reprimanded for two OSC violations.