Skip to content
Advertisement

Check if TinyMce field contains specific words

I have form and I want to check tiny mce field and prevent users from using some specific words. I get TinyMce field value but I can’t check if tiny_content value contains badwords

My html with TinyMce field

<div id="mceu_24" class="mce-tinymce mce-container mce-panel">
    <div id="mceu_31" class="mce-edit-area mce-container mce-panel mce-stack-layout-item">
        <iframe id="post_content_ifr">
            <html lang="en-US">
                <head></head>
                <body id="tinymce" class="mce-content-body post_content post-type-page>
                  <p>my test content abc bca dssdf </p>
                </body>
            </html>         
        </ifame>
    </div>
</div>

My jQuery

jQuery(function() {
   $(".directorypress-submit-form-section-content").on("click",function() {
      var badwords = ["abc", "bca", "hai hello"];
      var editor='content_textarea';
      var tiny_content =tinyMCE.activeEditor.getContent({format: "text"});
      alert(tiny_content);

      if($.inArray(tiny_content, badwords) !==-1)
        {
            alert("Your Message Contains Bad Words, Please Remove Them Before Proceeding");
            return false;
        }
      
  });
});

Advertisement

Answer

I use .some method. The some(..) method checks each element of the array against a test function and returns true if any element of the array passes the test function, otherwise, it returns false

jQuery(function() {
   $(".directorypress-submit-form-section-content").on("click",function() {
      var badwords = ["abc", "bca", "hai hello"];
      var editor='content_textarea';
      var tiny_content =tinyMCE.activeEditor.getContent({format: "text"});
      alert(tiny_content);

      let isFounded = badwords.some( ai => tiny_content.includes(ai) );
      if(isFounded == true)
      {
          alert("Your Message Contains Bad Words, Please Remove Them Before Proceeding");
          $(".submit-listing-button").removeClass("validate-button-class");
          return false;
      }
      else{
        $(".submit-listing-button").addClass("validate-button-class");
      }
      
  });
});
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement