I am looking for a javascript where in I paste a content in textarea. The script must search for the timestamp of the format 01/07/2016 10:30:00 and chop it off. Please help me in this. It has a text content in which I have to eliminate the above thing. My html would be something like this-
<textarea id="quote" cols="50" rows="5" onkeyup="myFunction()">
</textarea>
<script>
function myFunction() {
var x = document.getElementById("quote");
x.value = x.value.toUpperCase();
}
</script>
This is a regex approach which solves your problem.
var txtArea = document.getElementById('mytextarea');
txtArea.addEventListener('paste', function(e) {
e.preventDefault();
e.stopPropagation();
clipboardData = e.clipboardData || window.clipboardData;
pastedData = clipboardData.getData('Text');
var pattern = /[\d]{2}\/[\d]{2}\/[0-9]{4}\s[\d]{2}:[\d]{2}:[\d]{2}/g;
// remove date time pattern.
var newData = pastedData.replace(pattern, '');
// remove string 'LSDXW08'
newData = newData.replace(/LSDXW08/g, '');
// replace multiple spaces with 1 space
newData = newData.replace(/\s{2,}/g, ' ');
// append the value
txtArea.value = txtArea.value + newData;
});
<textarea id="mytextarea" rows="8" cols="70"></textarea>
<h4> !! Try paste this !! </h4>
<p>
I am looking for a javascript where in I paste a content in textarea. The script must search for the timestamp of the format 01/07/2016 10:30:00 and chop it off. Please help me in this. It has a text content in which I have to eliminate the above thing. My html would be something like this-
</p>
Try this regex
function myFunction() {
var x = document.getElementById("quote");
x.value = x.value.toUpperCase();
timestamp= x.value.match(/\d{2}\/\d{2}\/\d{4}\s\d{2}\:\d{2}\:\d{2}/g)
console.log(timestamp) //It will return array of timestamps found
}
Related
I want to make bad words filter but I have a problem with getting this code to work. It works in every other textareas but this one.
After a lot of searching I found that maybe this is because this textarea is with summernote. Any ideas what I should I do? Thanks in advance!
<div class="form-group">
<textarea rows="7" name="proposal_desc" placeholder="Въведете описание на предложението си" class="form-control proposal-desc"><?= $d_proposal_desc; ?></textarea>
</div>
<script>
$( document ).ready(function() {
var triggerWords = ['badword1', 'badword2'];
$(document).on('keyup', 'textarea', function() {
for (var i = 0; i < triggerWords.length; i++) {
if ($(this).val().toLowerCase().indexOf(triggerWords[i]) != -1) {
alert("Alert! You've typed a blocked word.");
}
}
});
});
</script>
You can simplify our code with mixed of plain JS.
const text = "I type badword1 word and will block."
const isBadWord = text.replace(/[\W_]+/g," ").split(' ').includes('badword1')
isBadWord ? alert('Bad word detected!') : null;
You can try it:
var regex = new RegExp('\\b(' + badwords.join('|') + ')\\b', 'i' );
What I want is filtering user's input. Remove newline and limit its length.
I tried two methods.
https://jsfiddle.net/mj111/356yx1df/3/
html
<div>
<textarea id="text1" placeholder="write down"></textarea>
</div>
<div>
<textarea id="text2" placeholder="write down"></textarea>
</div>
script
document.getElementById('text1')
.addEventListener('input', function (evt) {
evt.preventDefault(); // prevent value change
const msg = evt.target.value.replace(/\n/g, '')
if (msg.length <= 10) {
document.getElementById('text1').value = msg
}
})
document.getElementById('text2')
.addEventListener('input', function (evt) {
const msg = evt.target.value.replace(/\n/g, '').slice(0, 10)
document.getElementById('text2').value = msg
})
First one is not working, because preventDefault is not working. As MDN doc says. 'input' event is not cancelable.
So, I tried second method; just overwrite textarea value.
I think there's a better way to do this. If anyone has a good idea, please answer.
use keyup event to limit length or you can just add maxlength manually to the HTML as an attribute. If you want to set it dynamically you can use Element.setAttribute. For preventing new lines you can prevent the return key, as long as it is not shift. You can still use replace to replace things you feel need replacing, a regular expression will most effectively get the job done.
var text = document.getElementsByTagName('textarea');
var text1 = text[0];
var text2 = text[1];
text1.addEventListener('keydown', function(event) {
let val = event.target.value;
let limit = 25;
// limit the value once you reach a particular length
if (val.length > 3) {
event.target.value = val.substr(0, limit)
};
// prevent new line by preventing enter key press
if (event.keyCode === 13 && !event.shiftKey) {
event.preventDefault();
alert('no new lines please');
return false;
}
// and you can filter with replace
})
<div>
<textarea id="text1" placeholder="write down"></textarea>
</div>
<div>
<textarea id="text2" placeholder="write down"></textarea>
</div>
I have to make a script which can check my textarea if there are any bad words in there. Once the users leaves the textarea their should be alert box appearing. After that the inputted text in the area should be deleted.
I need JavaScript solutions (no jQuery please).
<!DOCTYPE html>
<html>
<head>
<title>RIC</title>
</head>
<body>
<textarea name="ric" id="textarea">
</textarea>
<script>
my_textarea = document.getElementById('textarea');
if (/\b(?=\w)(asshole|fucking)\b(?!\w)/i.test(my_textarea.value)) {
alert("Hey no bad words here!");
} else {
// Okay move on!
}
</script>
</body>
</html>
Here's what you can do. First create a function which will check for invalid words and clear the textarea:
function filterTextarea() {
var textarea = document.getElementById('textarea');
var matches = textarea.value.match(<THE REGEX FOR THE WORDS YOU WANT TO FILTER>);
if (matches) {
alert('Hey, no bad words!');
textarea.value = '';
return;
}
// Other stuff.
}
Then attach an event listener to the textarea, which will check it every time a key is pressed and pass your function as a callback.
document.getElementById('textarea').addEventListener('keyup', filterTextarea, false);
This code should work. It will also censor the words by replacing them with a group of asterisk characters.
<!DOCTYPE html>
<html>
<head>
<title>RIC</title>
</head>
<body>
<textarea name="ric" id="textarea" onblur="checkWords()"></textarea>
<script>
function checkWords() {
var my_textarea = document.getElementById('textarea').value;
var pattern = /fox|dog/ig;
if (my_textarea.match(pattern)) {
alert("Hey no bad words here!");
my_textarea = my_textarea.replace(pattern, "****" );
document.getElementById('textarea').value = my_textarea;
}
}
</script>
</body>
</html>
The reason your function does not get called (well it does get called, once at page load) is because there is no event handler to check when the user leaves the text area. You can either use onChange or onBlur they both trigger when the user leaves but onChange will only trigger when the content has actually been changed.
Try changing your code to this:
<textarea name="ric" id="textarea" onBlur="check()">
</textarea>
<script>
var check = function(){
my_textarea = document.getElementById('textarea');
if (/\b(?=\w)(asshole|fucking)\b(?!\w)/i.test(my_textarea.value)) {
alert("Hey no bad words here!");
my_textarea.value = "";
} else {
// Okay move on!
}}
</script>
As for the checking on bad words, as stated by others you can loop an array of 'bad words' with <text>.indexOf(<bad word>) and check if the index is found or not. There might be a 'nicer' way using regex but can't help with that
Here goes my code,Please do populate the badword array with your bad words and this code must oppose bad words ,it will.!!!
<div>
<textarea name="ric" id="txtArea" onkeyup="badWordChecker()" onblur="nothingTyped()"> </textarea>
</div>
<script>
function nothingTyped() {
var badWordTextBoxLength = document.getElementById('txtArea').value.length;
if (badWordTextBoxLength == 0) {
alert("YOu cannot leave easily!!!Please type something ");
document.getElementById('txtArea').focus();
return false;
}
}
function badWordChecker() {
//create an array of bad words
var badwords = ["f***", "a****", "f***"];
var badWordTextBox = document.getElementById('txtArea');
var badWordTextBoxValue = badWordTextBox.innerText;
var backgroundcolor = "white";
function isTheWordBad(value, index, array) {
if (value == badWordTextBoxValue) {
alert("hey!No badwords");
badWordTextBox.textContent = "";
return true;
}
else {
return false;
}
}
var result = badwords.filter(isTheWordBad);
}
</script>
</body>
Let's say I have a input field
<input type="text" class="form-control" name="filename">
and do write something like this:
Hällo
Is it possible to check while writing if that field has a the letter ä and change it to an a while writing ?
So far, I built this:
$search = array('ä', 'Ä');
$replace = array('ae', 'Ae');
$project = str_replace($search, $replace, $input);
You don't do this with PHP, you would do it with Javascript:
var el = document.getElementById("filename");
el.addEventListener('keyup', function(){
var replace = ['ä','å'];
var replacewith = 'a';
var replace1 = ['ö'];
var replacewith1 = 'o';
replace.forEach(function(letter){
el.value = el.value.replace(letter, replacewith);
});
replace1.forEach(function(letter){
el.value = el.value.replace(letter, replacewith1);
});
});
Add id="filename" to the input element for this to work. You can add as many letters to the replace array as you would like.
You can also add more arrays to replace letters.
https://jsfiddle.net/dek5as1x/1
Edit: Solution for several letters
var el = document.getElementById("filename");
el.addEventListener('keyup', function(){
var replaces = [['ä','å'],['ö','ø'],['ï','ì'],['ü','ù','û'],['ë','ê']];
var replacewith = ['a','o','i','u','e'];
replaces.forEach(function(letterGroup, index){
letterGroup.forEach(function(letter){
el.value = el.value.replace(letter, replacewith[index]);
});
});
});
Here you add a new array ([]) to replaces. Then you add all the letters to that array that should be turned into the same letter to that array. Example: ['ê','ë','è']. Then you add the letter to the replacewith array. It is important that the letter comes to replace the letters in the array has the same index as the corresponding array in replaces.
This solution should be a little cleaner for when you have lots of letters to replace.
You can't do this using PHP because PHP is serverside.
But you can do this using JavaScript.
Try:
<script language="JavaScript">
function replace() {
var input = document.getElementById("filename");
input.value = input.value.replace("ä","a");
}
<script>
<input type="text" class="form-control" name="filename" onchange="replace()">
I have 2 input fields in a html file, text1 and text2. Then I copy a long string and paste it into text1. I want the string splited automatically into text1 and text2. So there must be a delimiter e.g TAB (ASCII 9) in the string. I have been trying many times but no lucky. In my experiment, there is a button calling javascript function as follows :
<script>
function Chr(AsciiNum)
{
return String.fromCharCode(AsciiNum)
}
function test()
{
c = "ABC"+Chr(9)+"DEF";
document.getElementById("text1").value=c;
}
</script>
<input type="button" value="Paste it" onClick="test()">
What I want is text1 filled with ABC and text2 filled with "DEF"
Thanks you for your helps .....
Splitting is simple:
function test(pastedText) {
var parts = pastedText.split(Chr(9));
document.getElementById("text1").value = parts[0];
document.getElementById("text2").value =
(parts[1] === undefined ? "" : parts[1]);
}
The tricky part, actually is the pasting, check the full code below.
See a online DEMO for code here.
Text1: <input type="text" id="text1"><br />
Text2: <input type="text" id="text2"><br />
<br />
<div>Sample string (copy the red text and paste it on Text1):</div>
<div style="color:red">ABC DEF</div>
<script>
function Chr(AsciiNum) {
return String.fromCharCode(AsciiNum)
}
function test(pastedText) {
var parts = pastedText.split(Chr(9));
document.getElementById("text1").value = parts[0];
document.getElementById("text2").value = (parts[1] === undefined ?
"" : parts[1]);
}
/** HANDLING PASTE EVENT
* Credits to: http://stackoverflow.com/a/6035265/1850609 */
function handlePaste(e) {
var pastedText = undefined;
if (window.clipboardData && window.clipboardData.getData) { // IE
pastedText = window.clipboardData.getData('Text');
} else if (e.clipboardData && e.clipboardData.getData) {
pastedText = e.clipboardData.getData('text/plain');
}
test(pastedText); // Process and handle text...
return false; // Prevent the default handler from running.
};
document.getElementById("text1").onpaste = handlePaste;
</script>
I also suggest you rename the test() function into something more meaningful to you.
Why dont you just do like that:
c = "ABC "+Chr(9);
document.getElementById("text1").value=c;
document.getElementById("text2").value= "DEF";
This should be inside test()
Hope this helps.