1

I have an HTML input that I want to sanitize. I want users to enter everything except some characters. I don't know what those characters are, but users somehow enter them by copying and pasting. Sublime Text shows them as STX, SOH and so on. I don't know if you can see them, but here they are:" ". I don't want to list every character that I accept, nor I can list these STX, SOH characters. I can't use A-Za-z, because I also want users to enter characters like 'ç'.

How can I achieve this easily?

Here's a part of the -unfinished- code that I found on here:

function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  // var regex = /[0-9]|\./;
  var regex = /[a-zA-Z0-9\!\'\+\-\&\\\/\(\)\?\@\#\$\%\^\*\_\|]+/;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
        theEvent.preventDefault();
  }
}

Thank you very much in advance

  • I don't have a definitive answer, but I would start with Character Classes in regular expressions, particularly Unicode Categories: https://msdn.microsoft.com/en-us/library/20bw873z.aspx#CategoryOrBlock. (I am aware your question is about JS, but for inspiration) – Siderite Zackwehdex Apr 14 '16 at 13:25
  • [Have you tried](http://stackoverflow.com/a/24229554/1414562) `var regex = /[^\x20-\x7E]+/;` ? Or better would be to just replace the string on input event: `$('#inputToSanitize').on('input', function(){this.value = this.value.replace(/[^\x20-\x7E]+/g, ''); });`. Of course, this should be checked server side too. – A. Wolff Apr 14 '16 at 13:51
  • I don't know how many characters I have to add there. I wrote STX, SOH, but I'm sure there are lot's and lot's of characters like that. Like emoji's for example. – citizen_of_noobville Apr 14 '16 at 13:53
  • Is this only an HTML `input`, or are they actually copy-pasting into a `content-editable div`? I have had issues around this before with content-editable divs and copy-pasting. – KevBot Apr 14 '16 at 15:08

0 Answers0