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 tochange
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
3 Answers
Reset to default 3I 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);
});
});