jquerysummernote

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


I'm using summernote editor and what I want to do is to restrict the user from adding characters if the text area is pre filled, but I couldn't figure out how to restrict the user not to enter a single character in the text area. Here is my code

 $(document).ready(function() {
  $('#summernote').summernote({
   callbacks: {
     onKeydown: function(e) {
      if(e.keyCode > 64 && e.KeyCode < 122)
      {
         e.target.blur();
      }
    }
  }
 });
});

Solution

  • <!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();
          }
        }
      }
     });
    });