• So I use the “PreimumPress” Classifieds Blue child theme, and the code I have below will check if there is a “bad word” entered into the Keywords input/field and alerts the user about it once they click on the Save Listing button. The problem though is that it ONLY alerts the user when there is just ONE of the specified “bad words” in that field. It won’t trigger the alert when the “bad word” is found within a whole sentence. So I need help with two things…

    – Can anyone help me tweak this code so that it will trigger the alert when the “bad word” is found ANYWHERE within that field, even if there are other words.
    – Can anyone help me have this “alert” happen when the specified “bad words” are in ANY field, not just the “keywords” field.

    *For reference, my website is https://www.lakomai.com

    THANKS!!

    <script>
    jQuery(function() {
    jQuery("#MainSaveBtn").on("click",function() {
        var name = jQuery("input:text[name='custom[post_tags]']").val();
            var badwords = ["word1", "word2", "bad word3"];
    
            if(jQuery.inArray(name, badwords) !==-1)
                {
                    alert("Your Listing Contains Bad Words, Please Remove Them Before Proceeding");
                    return false;
                }
    });
    });
    </script>
Viewing 15 replies - 1 through 15 (of 22 total)
  • Hi,

    It appears that you’re using a commercial theme and volunteers don’t have access to commercial themes, so I will recommend you to post your question to the theme’s official support channel here: https://www.premiumpress.com/contact/

    www.remarpro.com forum is only for the free GPL themes. Hope you understand. ??

    Thread Starter nkpryor

    (@nkpryor)

    Hi Hardeep, I’ve already posted it there, but have received no useful help yet. This is more of a CODING question then a THEME-BASED question… Anyone who is familiar with coding should be able to help me answer this.

    Hi there,

    Sorry didn’t notice it’s something custom you wanted to do but I thought it was a something you were trying to modify in the theme. Anyway, this should work:

    <script>
    jQuery(document).ready(function($) {
    	$( "#MainSaveBtn" ).click(function() {
    		var name = $("input:text[name='custom[post_tags]']").val();
    		var badwords = ["word1", "word2", "bad word3"];
    
    		if($.inArray(name, badwords) !==-1) {
    			alert("Your Listing Contains Bad Words, Please Remove Them Before Proceeding");
    			return false;
    		}
    	});
    });
    </script>

    Let me know. ??

    Thread Starter nkpryor

    (@nkpryor)

    Hardeep,

    That code didn’t seem to work. I tried it on my site, but it still only works if the ONLY TEXT i put in the field int he “bad word”.

    For example… the alert will pop up if I write…

    “F***”

    but it will NOT pop up if I write…

    “Did you know that F*** is a bad word?”

    Basically… if the word is within a sentence, it no longer notices it, and therefore it does not create the “alert”. Do you think you can fix that in the code?

    This should work in such case:

    <script>
    jQuery(document).ready(function($) {
    	$( "#MainSaveBtn" ).click(function() {
    		var name = $("input:text[name='custom[post_tags]']").val();
    		var badwords = ["word1", "word2", "bad word3"];
    		var breakOut = false;
    
    		badwords.forEach(function(entry) {
    			if(name.indexOf(entry) !== -1){
    				breakOut = true;
    			}
    		});
    
    		if(breakOut == true) {
    			alert("Your Listing Contains Bad Words, Please Remove Them Before Proceeding");
    			return false;
    		}
    	});
    });
    </script>

    Let me know. ??

    UPDATE: Updated the code to fix the issue which stopped form from submitting if user previously used a bad word.

    Thread Starter nkpryor

    (@nkpryor)

    Hardeep, thanks for that! However, it now works for JUST the “Keywords” field, but I need it to search the “Title” field and the “Description” field too.

    Basically, it’s CURRENTLY only looking at the “custom[post_tags]” field… but I need it to simultaneously search “form[post_title]” and the “form[post_content]” field too. Are you able to tweak the code so that it does that simultaneously??

    Let me know if you need clarification. Thanks!

    Hi,

    Yes, more details will be helpful. ??

    Thread Starter nkpryor

    (@nkpryor)

    Hardeep, what details do you need? Could you please specify?

    You can see the exact fields I’m referring to if you click on the following link…

    https://www.lakomai.com/add-listing/

    To be more specific, I’m referring to the “Title” field, the “Description” field, and the “Keywords” field located on that link above. You might have to create an account in order to see the fields that I’m referring to. Feel free to ask me any question if you have them. Thanks!

    This should do it:

    <script>
    jQuery(document).ready(function($) {
    	$( "#MainSaveBtn" ).click(function() {
    		var title = $("input:text[name='form[post_title]']").val();
    		var description = $("textarea[name='form[post_content]']").val();
    		var tags = $("input:text[name='custom[post_tags]']").val();
    		var badwords = ["word1", "word2", "bad word3"];
    		var breakOut = false;
    
    		badwords.forEach(function(entry) {
    			if(title.indexOf(entry) !== -1 || description.indexOf(entry) !== -1 || tags.indexOf(entry) !== -1){
    				breakOut = true;
    			}
    		});
    
    		if(breakOut == true) {
    			alert("Your Listing Contains Bad Words, Please Remove Them Before Proceeding");
    			return false;
    		}
    	});
    });
    </script>

    Can you please remove my hardeepasrani account from your site? I had to register so I could see the listing page.

    Hope it works. ??

    Thread Starter nkpryor

    (@nkpryor)

    Hardeep! That seems to be working perfectly! Thanks for that! One last question… Is there a way to change the code so that it will highlight the badword that triggered the alert? Or maybe even include the bad word in the alert? For example, I’d like the alert to say something like this…

    Your Listing Contains Bad Words, Please Remove Them Before Proceeding.
    The word that triggered this alert is: “badword1”

    Is this doable?

    Yes, it is doable. This should add that:

    <script>
    jQuery(document).ready(function($) {
    	$( "#MainSaveBtn" ).click(function() {
    		var title = $("input:text[name='form[post_title]']").val();
    		var description = $("textarea[name='form[post_content]']").val();
    		var tags = $("input:text[name='custom[post_tags]']").val();
    		var badwords = ["word1", "word2", "bad word3"];
    		var breakOut = false;
    		var usedwords = [];
    		var count = 0;
    
    		badwords.forEach(function(entry) {
    			if(title.indexOf(entry) !== -1 || description.indexOf(entry) !== -1 || tags.indexOf(entry) !== -1){
    				breakOut = true;
    				if(breakOut == true) {
    					usedwords[count++] = entry;
    				}
    			}
    		});
    
    		if(breakOut == true) {
    			alert("Your Listing Contains Bad Words, Please Remove Them Before Proceeding.\nThe word(s) that triggered this alert: " + usedwords);
    			return false;
    		}
    	});
    });
    </script>

    Hope it completes your task. ??

    Thread Starter nkpryor

    (@nkpryor)

    Thread Starter nkpryor

    (@nkpryor)

    Hardeep, it looks like this code you gave me that creates an alert for “bad words” is case sensitive. For example, it will create the alert for “bad word”, but not for “BAD WORD”. Is there a way to edit the code so that it will trigger the alert regardless of capitalization? I would like it to NOT be “case sensitive”.

    I really appreciate your help!! Thanks!!

    Try replacing it with the following code:

    <script>
    jQuery(document).ready(function($) {
    	$( "#MainSaveBtn" ).click(function() {
    		if ($("input:text[name='form[post_title]']").length > 0) {
    			var title = $("input:text[name='form[post_title]']").val();
    			var title = title.toLowerCase();
    		} else {
    			var title = "";
    		}
    		if ($("textarea[name='form[post_content]']").length > 0) {
    			var description = $("textarea[name='form[post_content]']").val();
    			var description = description.toLowerCase();
    		} else {
    			var description = "";
    		}
    		if ($("input:text[name='custom[post_tags]']").length > 0) {
    			var tags = $("input:text[name='custom[post_tags]']").val();
    			var tags = tags.toLowerCase();
    		} else {
    			var tags = "";
    		}
    		var badwords = ["word1", "word2", "bad word3"];
    		var breakOut = false;
    		var usedwords = [];
    		var count = 0;
    
    		badwords.forEach(function(entry) {
    			if(title.indexOf(entry) !== -1 || description.indexOf(entry) !== -1 || tags.indexOf(entry) !== -1){
    				breakOut = true;
    				if(breakOut == true) {
    					usedwords[count++] = entry;
    				}
    			}
    		});
    
    		if(breakOut == true) {
    			alert("Your Listing Contains Bad Words, Please Remove Them Before Proceeding.\nThe word(s) that triggered this alert: " + usedwords);
    			return false;
    		}
    	});
    });
    </script>

    Make sure to put all your bad words in lowercase letters.

    Thread Starter nkpryor

    (@nkpryor)

    Hi Hardeep, is there a way to create the code so that my “bad words” don’t have to be in lowercase? The reason I ask is because I’m also using this code for people that create “spam” postings on my website. So what I do is I “copy and paste” various lines of text from spammers postings, so that if they try to post it again, the alert pops up and stops them.

    So yes, is there a way to make it so that the “bad words” dont have to be entered in lowercase?

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Need code that'll prevent posts with "bad words"’ is closed to new replies.