Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script>
$(function(){
let allComments = document.getElementById("all-comments");
let $name = $("#name");
let $comment = $("#comment");
$("#submit").on('click', function(){
//validation to prevent html tags being inputted input box
var reg =/<(.|\n)*?>/g;
if (reg.test($('#comment').val()) == true) {
var errorText ='HTML Tags Not Allowed';
alert(errorText);
}
else
{
var item =
{
username: $name.val(),
comment: $comment.val(),
postId: window.location.pathname,
};
$.ajax({
type: "POST",
url: "[your-api-url-here]]",
data: JSON.stringify(item),
contentType: "application/json",
success : function(){
alert("Comment submitted successfully")
location.reload();
},
error : function(err){
alert("error in post" + JSON.stringify(err))
}
});
}
$name.val("");
$comment.val("");
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment