Escape Single and double quotes from the text area even giving Crlt+V

Asked

Viewed 767 times

4

// I just don’t want to let you type in single quotes and double quotes

$('#bot').keypress(function (e) {
    var regex = new RegExp("^[a-zA-Z0-9-]+$");
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
        return true;
    }

    e.preventDefault();
    return false;
});

3 answers

14

The problem of keypress is that will not recognize if the data comes from other sources, IE, will not work with Ctrl+V or right mouse click.

I recommend first use .on('input') and instead of having to check character by character:

String.fromCharCode(!e.charCode ? e.which : e.charCode)

You can just use .replace in the .val() or .value

Thus:

$('#bot').on('input', function (e) {
   this.value = this.value.replace(/['"]/g, "");
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Digite algo: <textarea id='bot'></textarea>

This way it will work when typing and when using Ctrl+V, you will not need to add all events such as:

  • .on('keypress')
  • .on('paste')
  • 1

    was faster than me, +1 good :)

  • 1

    @If you have another suggestion, different of course, that also solves does not exist in reply :D can guarantee a few dots and still help many users.

  • 1

    @Guillhermenascimento resolves "much simpler" even.

1


You can use the following Regex:

$('#bot').on('keypress', function (e) {

    if (!/['"]/.test(String.fromCharCode(e.keyCode))) {
        return;
    } else {
        e.preventDefault();
    }
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Digite algo: <textarea id='bot'></textarea>

  • 1

    And the question of CTRL+V?

0

You can do it as follows, just by checking the quotes and simple quotes with a simple if:

$(function(){
  $('#bot').keypress(function (e) {      
      var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
      if (str == '"' || str == "'") {
          return false;
      }
      return true; 
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="bot"></textarea>

I don’t know what your concern is with quotation marks, but you can also replace them with HTML Entities, as follows:

$(function(){
  $('#bot').keypress(function (e) {      
      var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
      if (str == '"') {
      		$(this).html($(this).val() + '&quot;');
          return false;
      }
      if (str == "'") {
      		$(this).html($(this).val() + '&apos;');
          return false;
      }
      return true; 
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="bot"></textarea>

So the user can use the quotes and you will have the HTML Entities in place of quotation marks behind.

  • 1

    And the question of CTRL+V?

  • Yeah I didn’t think of that

Browser other questions tagged

You are not signed in. Login or sign up in order to post.