How to can restrict the user not to enter characters in the summernote textarea?

Viewed 18

I'm using summernote component and what I want to do is I want to stop the user from adding characters if the text area is pre filled, but I couldn't figure out how to stop typing event.

1 Answers

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Summernote Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.js"></script>
</head>
<body>

<div class="container mt-3">
 <div class="inner-container">
    <div id="summernote1">
          Hello Summernote ! please enter text.
     </div>
  </div>

</div>

<script type="text/javascript">
$(document).ready(function() {
  $('#summernote1').summernote({
   callbacks: {
     onKeydown: function(e) {
      if(e.keyCode > 0)
      {
         e.preventDefault();
      }
    }
  }
 });
});

</script>
</body>
</html>

I simply added this code in my html page and it worked for me.

$(document).ready(function() {
  $('#summernote1').summernote({
   callbacks: {
     onKeydown: function(e) {
      if(e.keyCode > 0)
      {
         e.preventDefault();
      }
    }
  }
 });
});
Related