Republican U.S. Senate candidate Roy Moore didn’t have the integrity to do the right thing and end his campaign following credible allegations of sexual misconduct. Fortunately, Alabama voters did it for him when they defeated the former judge at the polls on Tuesday.

Doug Jones, a Democrat and former civil rights and U.S. attorney, won the nationally watched race by about 20,000 votes out of more than 1.3 million cast — the first time a Democrat has won a Senate seat in the state in 25 years. Alabama voters showed the nation that a person so clearly unfit to hold office can and must be rejected.

Moore’s defeat was a blow to his highest-profile backer, President Donald Trump, who easily won the state in 2016. Jones’ victory also revealed clear-cut economic, educational, age and racial divides in one of the reddest of red states. In what the New York Times described as a “suburban shellacking,’’ white, well-educated women in major Alabama cities and suburbs soundly rejected Moore — even in some areas that had favored Trump.

Among white Alabama men, 72 percent voted for Moore, with 63 percent of white women supporting him, exit polls showed. But black voters, who turned out in high numbers, clearly helped push Jones to victory. A whopping 93 percent of black men voted for Jones, as did 98 percent of black women.

The vote was close, even though important reporting by the Washington Post uncovered several women with credible stories about how Moore dated and otherwise behaved inappropriately with underage girls years ago. Moore’s defeat further bolsters the “#MeToo” movement at a critical time in this nation’s history.

But that’s not the only reason voters made the right choice in Alabama. Moore also deserved to lose because of his racist, anti-gay, anti-Muslim views — ideas that got him removed from the bench twice. He’s clearly not the kind of person who should be serving in the Senate.

The unexpected victory by Jones sends a strong message to the nation. Alleged child molesters, who also trample on other values most Americans hold dear, are not welcome in leadership positions.