JSFiddle - React, Tailwind, and code Playground
HTML
This will prevent false positive for shift, ctrl or the tab key, but it won't handle ctrl+z or cmd+z:
<br /> <br />
<form id="my-form">
<input type="text" />
<br />
<select>
<option></option>
<option>0</option>
<option>1</option>
<option>2</option>
</select>
<br />
<textarea rows="5"></textarea>
<input type="checkbox" />
<input type="radio" name="radio" />
<input type="radio" name="radio" />
</form>
<button id="change-monitor">Ready for testing. Change an input.</button>
CSS
body,
button {
font-family: 'Arial';
font-size: 16px;
}
form {
width: 50%;
padding: 15px;
background-color: #dddddd;
margin-bottom: 15px;
}
form * {
width: 100%;
margin-bottom: 15px;
}
JavaScript
$('#my-form').on('change keyup paste', ':input', function(e) {
var keycode = e.which;
if (e.type === 'paste' || e.type === 'change' || (
(keycode === 46 || keycode === 8) || // delete & backspace
(keycode > 47 && keycode < 58) || // number keys
keycode == 32 || keycode == 13 || // spacebar & return key(s) (if you want to allow carriage returns)
(keycode > 64 && keycode < 91) || // letter keys
(keycode > 95 && keycode < 112) || // numpad keys
(keycode > 185 && keycode < 193) || // ;=,-./` (in order)
(keycode > 218 && keycode < 223))) { // [\]' (in order))
$('#change-monitor').html('Form has changed. Click to reset.');
}
});
$(document).on('click', '#change-monitor', function() {
$('#change-monitor').html('Ready for testing. Change an input.');
})