【问题标题】:disallow comments if they contain "http:" or "www." etc. in Javascript?如果评论包含“http:”或“www”,则不允许评论。等在Javascript中?
【发布时间】:2014-03-27 01:25:24
【问题描述】:

我正在开发一个聊天应用程序,并试图阻止垃圾邮件。我显然不希望人们发布指向其他网站的链接,所以如果 cmets 包含某些内容,例如“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;
        }
    }
}

【问题讨论】:

  • stackoverflow dot com 我刚刚破坏了你的“安全性”。绝对没有办法用代码过滤人类。
  • 但是有没有办法阻止人们输入 stackoverflow.com,这样其他用户就不容易复制和粘贴并将其放入自己的搜索栏中?我想让垃圾邮件发送者尽可能不方便。
  • slashdot&lt;b&gt;&lt;/b&gt;.&lt;b&gt;&lt;/b&gt;com

标签: javascript dialog chat alert spam


【解决方案1】:

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

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

确保也添加服务器端验证!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多