Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何禁止在表单中复制/粘贴?_Javascript_Forms_Meteor_Chat_Copy Paste - Fatal编程技术网

Javascript 如何禁止在表单中复制/粘贴?

Javascript 如何禁止在表单中复制/粘贴?,javascript,forms,meteor,chat,copy-paste,Javascript,Forms,Meteor,Chat,Copy Paste,我正在使用Meteor开发一个聊天应用程序,我不希望用户因为明显的垃圾邮件原因能够将内容复制/粘贴到表单中。这可能吗?以下是我用来运行聊天应用程序的代码: Javascript: // render all of our messages in the ui Template.chatBox.helpers({ "messages": function() { return chatCollection.find(); } }); // get the value for ha

我正在使用Meteor开发一个聊天应用程序,我不希望用户因为明显的垃圾邮件原因能够将内容复制/粘贴到表单中。这可能吗?以下是我用来运行聊天应用程序的代码:

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;
          }

            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;
          }

            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;
        }
    }
}
//在ui中呈现所有消息
Template.chatBox.helpers({
“消息”:函数(){
返回chatCollection.find();
}
});
//获取handlerbar helper用户的值
Template.chatMessage.helpers({
“用户”:函数(){
如果(this.userId==“me”){
返回this.userId;
}else if(this.userId){
getUsername(this.userId);
返回Session.get('user-'+this.userId);
}否则{
返回“匿名-”+this.subscriptionId;
}
}
});
//在向收藏发送消息时单击Send Chat(发送聊天记录)
Template.chatBox.events({
“单击#发送”:函数(){
if(Meteor.user()==null){
警报(“您必须登录到post”);
返回;
}
$('#messages')。动画({“scrollTop”:$('#messages')[0]。scrollHeight},“fast”);
var message=$(“#聊天信息”).val();
//检查消息中是否包含任何字符
if(message.length<1){
警报(“您必须输入要发布的消息”);
返回;
}
chatCollection.insert({
userId:'我',
信息:信息
});
$(“#聊天信息”).val(“”);
//验证
var bot=检查_bots();
如果(bot==false)
{    
//将消息添加到流中
发出('chat',消息);
}
其他的
{
警惕(“慢下来!不需要那么快发布”);
返回false;
}
},
“按键#聊天信息”:功能(e){
if(Meteor.user()==null){
警报(“您必须登录到post”);
返回;
}
如果(e.which==13){
//验证
var bot=检查_bots();
如果(bot==false)
{
$('#messages')。动画({“scrollTop”:$('#messages')[0]。scrollHeight},“fast”);
log(“您按下回车键”);
e、 预防默认值();
//在此处重复#发送单击事件的功能
var message=$(“#聊天信息”).val();
//检查消息中是否包含任何字符
if(message.length<1){
警报(“您必须输入要发布的消息”);
返回;
}
chatCollection.insert({
userId:'我',
信息:信息
});
$(“#聊天信息”).val(“”);
//将消息添加到流中
发出('chat',消息);
}
其他的
{
警惕(“慢下来!不需要那么快发布”);
返回false;
}
}
}
});
chatStream.on('chat',函数(消息){
chatCollection.insert({
userId:this.userId,
subscriptionId:this.subscriptionId,
信息:信息
});
});
var lastintime=0;
var差异=0;
var msg_count=0;
功能检查_bots()
{
var seconds=new Date().getTime()/1000;
秒=parseInt(秒);
如果(最后时间<秒)
{
差异=秒-上次时间;
lastintime=秒;
如果(差异=3)
{
返回true;
}
其他的
{
返回false;
}
}
}

我不知道从哪里开始。如何防止复制/粘贴

这不是个好主意。Internet Explorer有一个onpaste事件,也有一个,但一般来说,它很混乱,跨越了web设计中通常不应该跨越的界限,不可能在所有浏览器中完全实现,并且可以避免太多麻烦


设置字符率限制并检测垃圾邮件的危险信号(如高链接密度和重复)是一个更好的主意

好的,我将只设置一个字符限制,不允许链接。我认为这应该可以消除大多数垃圾邮件发送者。谢谢你的想法,这有帮助!垃圾邮件检测本身就是一门艺术。我看到的一件有趣的事情是一个蜂蜜陷阱;一个常规的输入框,使用css对人类隐藏,但机器人会自动填充。如果这个领域有内容,你就知道它是一个机器人。谢谢,这确实是个好主意。我也得试试。很高兴看到你使用我的代码:)