There are at least two ways to do this, one involves using the pattern attribute, a non-JavaScript solution, that certainly works; see solutions others provide below. But, if you need to really be more restrictive, here is a JavaScript solution that stops the user in their tracks from submitting invalid data.
HTML:
<form>
<label>Enter 1, 2, or x</label>
<input id="myinput" maxlength="1" type="text">
<input type="submit"><input type="reset">
</form>
JavaScript:
myinput.oninput = function() {
if (myinput.value != 1 && myinput.value != 2 && myinput.value != 'x') {
alert("Invalid: " + myinput.value);
}
};
Full working example here
A good resource that explores the oninput and other events: https://javascript.info/events-change-input