There are 32 states (plus the District of Columbia) that require electors to vote for a pledged candidate. Most of those states (19 plus DC) nonetheless do not provide for any penalty or any mechanism to prevent the deviant vote from counting as cast. Four states provide a penalty of some sort for a deviant vote, and 11 states provide for the vote to be canceled and the elector replaced (two states do both).
The Uniform Law Commission has drafted and recommended a law called the Uniform Faithful Presidential Electors Act that provides for electors to pledge to vote for a candidate, and for them to be replaced with an alternate in the event that they do not vote as pledged. As of October 2019, that Act has been adopted by Indiana, Minnesota, Montana, Nebraska, Nevada, and Washington.
The constitutionality of some of these laws was recently cast into doubt by the 10th circuit court of appeals in Baca v. Colorado Department of State, No. 18-1173 (10th Cir. 2019), which found that states may not penalize electors or cancel their votes when the electors vote for a candidate other than their party's nominee. However, Baca did not apparently call into question the right of states to require electors to pledge to vote for their party's nominee, which the Supreme Court upheld in Ray v. Blair, 343 U.S. 214 (1952).