There are 30 states (plus the District of Columbia) that require electors to vote for a pledged candidate. Most of those states (21 plus DC) nonetheless do not provide for any penalty or any mechanism to prevent the deviant vote from counting as cast. Five states provide a penalty of some sort for a deviant vote, and six 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 December 2016, that Act has been adopted by Minnesota, Montana, Nebraska, and Nevada.