I am trying to restrict a user from being able to enter whitespace or hit the spacebar after characters (unless they enter a comma) as well as restrict all special characters except numbers and letters.
The desired output would be something like: "ab, c" or "abc, def, g, h..." The user would only be allowed to have whitespace or hit the spacebar after a comma. A comma is the only special character permitted.
What I have so far but it's only allowing for 1 comma:
function checkKey() {
var clean = this.value.replace(/[^0-9,]/g, "")
.replace(/(,.*?),(.*,)?/, "$1");
// don't move cursor to end if no change
if (clean !== this.value) this.value = clean;
}
// demo
document.querySelector('input').oninput = checkKey;