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?
Asked
Active
Viewed 6,177 times
0
-
You will need to use JavaScript to achieve this. This will help you with the emojis: http://stackoverflow.com/questions/10992921/how-to-remove-emoji-code-using-javascript – thedp Nov 16 '16 at 09:25
-
Try `pattern="(?!.*(?:[\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])).*"` – Wiktor Stribiżew Nov 16 '16 at 09:30
-
Browser is stating that isn't a valid pattern @WiktorStribiżew – PI. Nov 16 '16 at 09:33
-
Ah yes, because you must be testing in FF or Chrome, and they are already using the `/u` tag. – Wiktor Stribiżew Nov 16 '16 at 09:34
-
@PI: But you didn't mention anything related to browser restrictions in your question. – Jongware Nov 16 '16 at 09:39
-
Try `pattern="(?!.*[\u{1F600}-\u{1F6FF}\u2600-\u26FF\u200B]).*"`. BTW, what emojis are you trying to disallow? All in http://www.unicode.org/emoji/charts-beta/full-emoji-list.html? – Wiktor Stribiżew Nov 16 '16 at 09:39
1 Answers
-2
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/

thedp
- 8,350
- 16
- 53
- 95
-
2I just tested and was able to paste an Emoji into that JSFiddle. I don't think it worked as expected. – bytor99999 Mar 08 '19 at 21:18
-
3@user1567291 This solution wouldn't work if you're pasting into a field rather than typing directly. Try replacing `onkeypress` with `onpaste` – Mike Oct 07 '19 at 11:45