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
JavaScript
x
13
13
1
<div id="mceu_24" class="mce-tinymce mce-container mce-panel">
2
<div id="mceu_31" class="mce-edit-area mce-container mce-panel mce-stack-layout-item">
3
<iframe id="post_content_ifr">
4
<html lang="en-US">
5
<head></head>
6
<body id="tinymce" class="mce-content-body post_content post-type-page>
7
<p>my test content abc bca dssdf </p>
8
</body>
9
</html>
10
</ifame>
11
</div>
12
</div>
13
My jQuery
JavaScript
1
16
16
1
jQuery(function() {
2
$(".directorypress-submit-form-section-content").on("click",function() {
3
var badwords = ["abc", "bca", "hai hello"];
4
var editor='content_textarea';
5
var tiny_content =tinyMCE.activeEditor.getContent({format: "text"});
6
alert(tiny_content);
7
8
if($.inArray(tiny_content, badwords) !==-1)
9
{
10
alert("Your Message Contains Bad Words, Please Remove Them Before Proceeding");
11
return false;
12
}
13
14
});
15
});
16
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
JavaScript
1
21
21
1
jQuery(function() {
2
$(".directorypress-submit-form-section-content").on("click",function() {
3
var badwords = ["abc", "bca", "hai hello"];
4
var editor='content_textarea';
5
var tiny_content =tinyMCE.activeEditor.getContent({format: "text"});
6
alert(tiny_content);
7
8
let isFounded = badwords.some( ai => tiny_content.includes(ai) );
9
if(isFounded == true)
10
{
11
alert("Your Message Contains Bad Words, Please Remove Them Before Proceeding");
12
$(".submit-listing-button").removeClass("validate-button-class");
13
return false;
14
}
15
else{
16
$(".submit-listing-button").addClass("validate-button-class");
17
}
18
19
});
20
});
21