Tinymce - How to turn it into a required field?

Asked

Viewed 294 times

2

I’m using a field of TextArea with the TinyMCE for text posting and would like to know if it is possible to add some validation so that it does not accept blank values.

I’ve tried to add the required of HTML5, but when I try to send the post, the field does not send and also does not display the error, whether it is filled or not.

html

<textarea type="text" name="texto"></textarea>  

script tinymce

  <script src="tinymce/js/tinymce/tinymce.js"></script>
   <script>tinymce.init({
        selector: 'textarea',
        height: 245,
        theme: 'modern',
        plugins: [
        'advlist autolink lists link image charmap print preview hr anchor pagebreak',
        'searchreplace wordcount visualblocks visualchars code fullscreen',
        'insertdatetime media nonbreaking save table contextmenu directionality',
        'emoticons template paste textcolor colorpicker textpattern imagetools'
        ],
        toolbar1: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
        toolbar2: 'print preview media | forecolor backcolor emoticons',
        image_advtab: true,
        language : "pt_BR",
   forced_root_block : false,
   force_br_newlines : true,
   force_p_newlines : false
    });
    </script>
  • It would be possible to add the part of the code referring to what you are trying to do?

  • You can validate via javascript

1 answer

2


To do this I think it is not enough to just add one required in HTML.
You’d have to create a code something like this:

$('#enviar').click(function(){
   if($('.requerido').val() == ''){
      alert('O textarea não pode estar em branco!');
   } else {
      // Envia dados
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<textarea class="requerido"></textarea>
<button id="enviar">Enviar</button>

  • The answer is perfect, thank you. However, me campo is not a button, it is an input like Submit. When fired, the form shows Alert, as the empty field or not, and even then sends the blank data. How to act?

  • Adds a else to the code and calls the function responsible for sending the data within the else. So if the field is empty, it shows Alert, otherwise (else) calls the function and sends the data. if($('.requerido').val() == ''){ alert('O textarea não pode estar em branco!'); } else { //chama a função que envia os dados }.

Browser other questions tagged

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