Prevent multiple submit of a textarea data - javascript

I have a textarea validation code that only submits when the textarea is not empty. The below code works perfectly but how can I prevent the content in the textarea from submitting multiple times pressing return on the keyboard multiple times in succession?
document.getElementById('input').addEventListener('keyup', function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13 && $.trim($(this).val())) {
e.preventDefault();
send();
}
});

window.formWasSubmitted = false;
document.getElementById('input').addEventListener('keyup', function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13 && $.trim($(this).val()) && !window.formWasSubmitted) {
e.preventDefault();
window.formWasSubmitted = true;
send();
}
});

Related

no extra row after enter in textarea

I have a text-area with no text inside, if i hit enter the cursor move's to the second row and blinking.
I put some code inside to stop it but still nothing the text-area moves down. example:
function areaOnkeydown(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code === 13) {
if (_text.value === '') {
return; // if there's no text and hit enter.. // return but move to second row
} else {
// do something here..
}
}
}
With 2 words on enter if text-area doesn't have any text remain as is do nothing.
Thanks in advance.
Just cancel the event when the value is empty. This is done using Event.preventDefault() function.
function areaOnkeydown(e) {
var code = (e.keyCode ? e.keyCode : e.which);
var value = e.target.value
if (code === 13) {
if (value === '') {
e.preventDefault()
return;
} else {
// update: added this else 28/11/2022
// so something
}
}
}
textarea.addEventListener("keydown", areaOnkeydown)
<textarea id="textarea" rows="4"></textarea>
Solved. I post the here the solution that works for me. No more new empty line on enter after text in text-area.
function areaOnkeydown(e) {
var code = (e.keyCode ? e.keyCode : e.which);
var value = e.target.value;
if (code === 13) {
if (value === '') {
if (value && value.indexOf('\n') > -1) {
e.preventDefault();
return;
}
e.preventDefault();
return;
}
onSendText(e); // also i put the e.preventDefault(); here
}
}
Thanks all for the help!

If a key is pressed press another one too in Javascript

I want to do the following in javascript :
If "B" is pressed > Press "C" automatically
How to achieve this condition ? e.keyCode == 66 is B
$(document).ready(function() {
$(document).bind("keydown", function(e) {
if (e.keyCode == 66) {
}
});
});
$(document).ready(function() {
$(document).on("keypress", e => {
if (e.which == 66) {
e.preventDefault();
var f = $.Event("keypress", {which: 13});
$(document).trigger(f);
}
});
});

Tabs default action is not stopped event though i use e.preventDefault()

I am showing a text area in a modal when I pressed tab it moves to the next input I wrote code to stop this but I didn't work for me.(when I pressed tab the execution even not coming to the acceptTabsSpace function)
$(document).on("keyup", "#collection-text-input", acceptTabsSpace);
function acceptTabsSpace(e){
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
e.preventDefault();
return false;
}
}
Add the listener to trigger on keydown instead:
$(document).on("keydown", "#collection-text-input", acceptTabsSpace);
function acceptTabsSpace(e){
var keyCode = e.keyCode || e.which;
if (keyCode === 9) {
e.preventDefault();
return false;
}
}
Use keydown instead key up and for tabspaces press tab twice.
$(document).on("keydown", "#collection-text-input", acceptTabsSpace);
function acceptTabsSpace(e) {
var keyCode = e.keyCode || e.which;
if (e.keyCode === 9) {
e.preventDefault();
this.value = this.value.substring(0, this.selectionStart) + "\t" +
this.value.substring(this.selectionEnd);
this.selectionEnd = s + 1;
}
}

Disable Ctrl key in IE 8

I want to disable the ctrl key in the IE browser.I had tried some solution using javascript but nothing is working can someone please help me to find out the solution
document.onkeydown = function () {
if (event.keyCode == 17) alert('Ctrl Key is disabled');
};
document.onkeydown = function(e) {
if (e.altKey && (e.keyCode === 36)) {//Alt+home blocked.
return false;
}
if (e.altKey && (e.keyCode === 70)) {//Alt+f blocked.
return false;
}
};
function hookKeyboardEvents(e) {
// get key code
var key_code = (window.event) ? event.keyCode : e.which;
// case :if it is IE event
if (window.event)
{
if (!event.shiftKey && !event.ctrlKey) {
window.event.returnValue = null;
event.keyCode = 0;
}
}
// case: if it is firefox event
else
e.preventDefault();
}
window.document.onkeydown = hookKeyboardEvents;
function Disable_Control_C() {
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
if (event.ctrlKey && (keystroke == 'c' || keystroke == 'v' || keystroke == 'p' || keystroke == 's' || keystroke == 'u')) {
alert("this function is disabled");
event.returnValue = false; // disable Ctrl+C
}
}
<body onkeydown="javascript:Disable_Control_C()">
this is what i do it to run in the IE...

How to prevent Chrome open history after press CTRL+H

I mean is there any way to prevent the default accesskey in Chrome.
var text = document.getElementById("text");
text.onkeyup = function(e) {
if(e.ctrlKey && e.keyCode == 72) {
// do something...
alert("You wont see me cause Chrome will open history manager");
}
}
<textarea id="text"></textarea>
This should work. You need Keydown Event.
var text = document.getElementById("text");
text.addEventListener("keydown", function(e) {
console.log(e.keyCode);
if (e.keyCode == 72 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
alert('Stopped');
}
}, false);
<textarea id="text"></textarea>
I've added isCtrlDown variable and keyup with keydown event, to achieve what you're looking for because I didn't see isKeyDown kind of function in Key as discussed here.
var isCtrlDown = false;
var text = document.getElementById("text");
text.onkeydown = function(e){
if(e.keyCode == 17){
isCtrlDown = true;
}
if(isCtrlDown && e.keyCode == 72){
// do something...
console.log("You wont see me cause Chrome will open history manager");
}
e.preventDefault();
}
text.onkeyup = function(e){
if(e.keyCode == 17){
isCtrlDown = false;
}
e.preventDefault();
}

Categories