Jump to content

Programmatically check comment text before posting

Recommended Posts

Below is the standard Squarespace HTML to post comments.
Would like to insert JavaScript to search the comment text for forbidden words or phrases, e.g., spam URLs, and if found refuse to post the comment.
How to do so?
Thanks.

<!--NEW COMMENT TOP-->
<div class="new-comment-area top-level-comment-area edit-mode">
<div class="input">
    <form action="https://www.manhattancontrarian.com/blog/2023-2-4-in-new-york-a-very-slow-political-awakening#" method="post" class="comment-form">
      <textarea class="comment-input" name="comment" data-defaulttext="Leave a comment here." data-edited="false"></textarea>
    </form>
  </div>
  <div class="comment-btn-wrapper">
    <span class="btn-text preview-comment top-level-preview-btn">Preview</span>
    <span class="comment-btn sqs-system-button sqs-editable-button-font sqs-editable-button-shape sqs-button-element--primary">
      Post Comment…
    </span>    
  </div>
</div>

 

Edited by FrankNatoli
Link to comment
  • Replies 1
  • Views 215
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

I've tweaked the above Squarespace HTML to include a new "Post" button and a JavaScript function to check a list of forbidden words or phrases, which in the actual implementation would include the spam URLs. Question now is what is the proper syntax to invoke the "Post Comment" button in the original Squarespace HTML inside my JavaScript? Thanks

<!DOCTYPE html>
<html>
<div class="new-comment-area top-level-comment-area edit-mode">
  <div class="input">
    <form action="https://www.manhattancontrarian.com/blog/2023-2-4-in-new-york-a-very-slow-political-awakening#" method="post" class="comment-form">
      <textarea class="comment-input" id="comment" name="comment" data-defaulttext="Leave a comment here." data-edited="false" rows="10" cols="80"></textarea>
    </form>
  </div>
  <div class="comment-btn-wrapper">
    <span class="btn-text preview-comment top-level-preview-btn">Preview</span>
    <span class="comment-btn sqs-system-button sqs-editable-button-font sqs-editable-button-shape sqs-button-element--primary">Post Comment…</span>    
  </div>
  <button onclick="CheckComment()">Post</button>
</div>
<script type="text/javascript">
var verbotenList = ["forbidden1", "forbidden2"];
function CheckComment() {
  var commentText = document.getElementById("comment").value;
  alert(commentText);
  var verbotenFound = 0;
  for (var i = 0; i < verbotenList.length; i++) {
    if (commentText.search(verbotenList[i]) >= 0) {
      verbotenFound++;
    }
  }
  alert("verbotenFound=" + verbotenFound);
  if (verbotenFound == 0) {
    alert("comment approved");
  } else {
    alert("comment not approved");
  }
}
</script>
</body>
</html>

Edited by FrankNatoli
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.