最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript - Prevent users from copy pasting a special character in input text? - Stack Overflow

programmeradmin1浏览0评论

In javascript how can i check if the users have '=' in the text they are pasting and prevent the text from being pasted in textbox. I found it a bit tricky. I am working on a vulnerability issue where users should not be allowed to enter = in input field where i have achieved to some extent where user cant enter in textfield but can copy paste which is an issue. Below is my code. Please help.

$(document).ready(function() 
{
    inPutCheckForText();
 });

function inPutCheckForText()
{
    fields = document.getElementsByTagName('input');
    for (index = 0; index < fields.length; ++index) 
    {
        fields[index].addEventListener("keydown", function(event) 
        {
            if (event.keyCode === 187) 
            {
                console.log("blocked.");
              event.preventDefault();
              return false;
            }
        });
    }
}

In javascript how can i check if the users have '=' in the text they are pasting and prevent the text from being pasted in textbox. I found it a bit tricky. I am working on a vulnerability issue where users should not be allowed to enter = in input field where i have achieved to some extent where user cant enter in textfield but can copy paste which is an issue. Below is my code. Please help.

$(document).ready(function() 
{
    inPutCheckForText();
 });

function inPutCheckForText()
{
    fields = document.getElementsByTagName('input');
    for (index = 0; index < fields.length; ++index) 
    {
        fields[index].addEventListener("keydown", function(event) 
        {
            if (event.keyCode === 187) 
            {
                console.log("blocked.");
              event.preventDefault();
              return false;
            }
        });
    }
}
Share Improve this question edited Feb 15, 2019 at 6:40 sTg asked Feb 15, 2019 at 6:28 sTgsTg 4,44418 gold badges74 silver badges125 bronze badges 2
  • instead of keydown listen to change event. In the callback see if you have = in your value, if you do remove it – Umair Abid Commented Feb 15, 2019 at 6:48
  • To strip specific chars from pasted text, you could set a handler for the paste-event (see: developer.mozilla/en-US/docs/Web/API/Element/paste_event), but this will not fix your vulnerability! A script could still insert those chars. You need to validate the input-data when you use it. NEVER trust any data from a source that unknowns have access to. – Superole Commented Nov 11, 2019 at 11:48
Add a ment  | 

3 Answers 3

Reset to default 3

I do it like this:

<input type="text" onpaste="validatePaste(this, event)"> 

                        

, and I call my function located inside <script> tags like:

function validatePaste(el, e) {
  var regex = /^[a-z .'-]+$/gi;
  var key = e.clipboardData.getData('text')
  if (!regex.test(key)) {
    e.preventDefault();
    return false;
  }
}

In addition to "keydown" event. you should use "paste" event to handle copy paste

$('#textbox').on("paste", function(e) {
  if (event.keyCode === 187) 
  {
    console.log("blocked.");
    event.preventDefault();
    return false;
  }
});

or try using

$ ('#textbox').bind("paste", function (e) {
  if (event.keyCode === 187) 
  {
    console.log("blocked.");
    event.preventDefault();
    return false;
  }
});

This will Work of Past event only, you can change it for other events. It will restrict a user to Past '=' in the input field

$(function(){
        $("#textInput").bind('paste', function(e) {
        var ctl = $(this);
        setTimeout(function() {
            //Do whatever you want to $(ctl) here....
            var bla = $('#textInput').val();
            if (bla.indexOf('=') > -1){
               alert("= not allowed");
                $( '#textInput' ).val(null) ;
                e.preventDefault();
                return false;
            }
        }, 100);
        });


    });
发布评论

评论列表(0)

  1. 暂无评论