如果注释在 Javascript 中包含"http:"或"www."等,则不允许注释?



我正在开发一个聊天应用程序,我正在努力防止垃圾邮件。我显然不希望人们发布到其他网站的链接,所以我不允许评论,如果他们包含某些东西,如"http:","https:","www。"。com"。net"。org"等等。如果评论包含这些类型的单词,是否可以弹出一个警告框?下面是我用来运行聊天应用程序的代码:

Javascript:

// render all of our messages in the ui
Template.chatBox.helpers({
  "messages": function() {
    return chatCollection.find();
  }
});
// get the value for handlerbar helper user
Template.chatMessage.helpers({
  "user": function() {
    if(this.userId == 'me') {
      return this.userId;
    } else if(this.userId) {
      getUsername(this.userId);
      return Session.get('user-' + this.userId);
    } else {
      return 'anonymous-' + this.subscriptionId;
    }
  }
});
// when Send Chat clicked at the message to the collection
Template.chatBox.events({
    "click #send": function() {
            if (Meteor.user() == null) {
              alert("You must login to post");
            return;
          }
            $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
            var message = $('#chat-message').val();
            // check to see if the message has any characters in it
            if (message.length < 1) {
              alert("You must enter a message to post.");
            return;
          }
            if (message.length > 200) {
              alert("Your message is too long... they can't read that fast!");
            return;
          }
            chatCollection.insert({
                userId: 'me',
                message: message
            });
            $('#chat-message').val('');
            //Validation
            var bot =Check_bots();
            if(bot==false)
            {    
            //add the message to the stream
            chatStream.emit('chat', message);
       }
        else
        {
            alert("Slow down! No need to post that fast.");
            return false;
        }
    },
    "keypress #chat-message": function(e) {
        if (Meteor.user() == null) {
            alert("You must login to post");
            return;
        }
        if (e.which == 13) {
          //Validation
       var bot =Check_bots();
        if(bot==false)
        {
            $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
            console.log("you pressed enter");
            e.preventDefault();
            //repeat function from #send click event here
            var message = $('#chat-message').val();
            // check to see if the message has any characters in it
            if (message.length < 1) {
              alert("You must enter a message to post.");
            return;
          }
          if (message.length > 200) {
              alert("Your message is too long... they can't read that fast!");
            return;
          }
            chatCollection.insert({
                userId: 'me',
                message: message
            });
            $('#chat-message').val('');
            //add the message to the stream
            chatStream.emit('chat', message);
        }
        else
        {
            alert("Slow down! No need to post that fast.");
            return false;
        }
    }
  }
});
chatStream.on('chat', function(message) {
  chatCollection.insert({
    userId: this.userId,
    subscriptionId: this.subscriptionId,
    message: message
  });
});
var lastintime=0;
var defference=0;
var msg_count=0;
function Check_bots()
{
    var seconds = new Date().getTime() / 1000;
    seconds=parseInt(seconds);
    if(lastintime < seconds)
    {
        defference = seconds -lastintime;
        lastintime=seconds;
        if(defference<=5 && msg_count>=3)
        {
            return true;
        }
        else
        {
             return false;
        }
    }
}

在您的click #send函数中添加:

if(message.match(/(http:|https:|www.|.com)/gi).length > 0) {
    alert("You are not allowed to use links");
    return;
}

一定要添加服务器端验证!

最新更新