Skip to content Skip to sidebar Skip to footer

Prevent Emoji And Zero Width Characters In Text Input

Is their a regex which I can use within the pattern attribute on a input tag to prevent people adding emojis and zero width spaces?

Solution 1:

document.getElementById('myinput').onkeypress = function() {
    var char = String.fromCharCode(event.which);   
    if (char.match(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])|\s/)) {
            event.preventDefault();
    }
};

See my JSFiddle: https://jsfiddle.net/0juLfy4g/1/


Post a Comment for "Prevent Emoji And Zero Width Characters In Text Input"