2

enter image description here

my question is probably simple for most but its troubling me and ive been googling for sometime now. I dont want allow copy or paste content and emoji characters in input fields.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
ABC
  • 41
  • 1
  • 5
  • 2
    Is there a reason? Just making sure this isn't an XY problem. – Eliott Robson Jan 31 '19 at 08:24
  • 1
    It's probably more useful to flip that - what do you want to *allow*. The technique for not allowing specific things is called "blacklisting" where you cannot use the explicitly blacklisted items. When you *only allow* specific items that's known as "whitelisting". In general, blacklisting is less effective, as you have to explicitly name every single thing you don't want, whereas when whitelisting, you just have to know what you *do* want. – VLAZ Jan 31 '19 at 08:25
  • 1
    What have you tried? If you've been googling, can you share what you've found that doesn't work for you? – Kaddath Jan 31 '19 at 08:25
  • I agree with VLAZ. You'd have to use an extensive library or make some fancy regex to change for ALT-Codes to disallow them. Rather work with a simple whitelist, e.g. `[^A-Za-z0-9]` – oel Jan 31 '19 at 08:29
  • I have added image, I dont want allow emoji symbols in input field – ABC Jan 31 '19 at 08:37
  • By default, `input` fields allow any character. Considering UTF-8 is allowed, this means thousands of characters, including hundred of emojis. The rule of what is allowed and what isn't will depend on what your field is about. If it makes sense to just filter out emojis, and allow everything else, you will need to exclude the Unicode ranges corresponding to emojis. Some information here: https://stackoverflow.com/questions/24840667/what-is-the-regex-to-extract-all-the-emojis-from-a-string – KevinLH Jan 31 '19 at 09:26

1 Answers1

2

var checkTf = function() {

  let charcode_max = 255;

  let tf = document.getElementById("tf");

  for (let i = tf.value.length - 1; i >= 0; i--) {
    if (tf.value[i].charCodeAt(0) > charcode_max) {
      tf.value = tf.value.substr(0, i) + tf.value.substr(i + 1);
    }
  }

}
<html>
<head></head>
<body>
  <textarea id="tf" oninput="checkTf()"></textarea>
</body>
</html>

You may increase charcode_max.

https://jsfiddle.net/qx4dt5bz/

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38
Julius S.
  • 664
  • 9
  • 21
  • Thanks! Its working fine. But I found some this regular expression: $(this).val($(this).val().replace(/[^a-z0-9]/gi, '')); But in this, Its not allowing to space. But I want to allow space as well excluding special characters and emoji characters. – ABC Feb 14 '19 at 08:17