Paste a treated text where the pointer is

2

I have the following problem:

  

Some people in my work copy texts from a few words and paste on the forms   that I do (inside the GED), but sometimes in this copy and paste it take with the text some invalid characters, for example   this quote: "

I had the idea of capturing the necklace, treating it and then pasting the treated text. Here is the code:

$(document).ready(function(){
	var clipboardData, pastedData;
	$(":input").on("paste", function (e) {
            e.preventDefault();
			clipboardData = e.clipboardData || window.clipboardData;
    		pastedData = clipboardData.getData('text');
			// smart single quotes and apostrophe
			pastedData = pastedData.replace(/[\u2018\u2019\u201A]/g, "\'");
			// smart double quotes
			pastedData = pastedData.replace(/[\u201C\u201D\u201E]/g, "\"");
			// ellipsis
			pastedData = pastedData.replace(/\u2026/g, "...");
			// dashes
			pastedData = pastedData.replace(/[\u2013\u2014]/g, "-");
			this.value += pastedData;
	});
});

I used paste when I tried the "value +=" but in that case, whenever someone places the pointer in the middle of an already typed text, it will ignore where the pointer is and paste the content of the paste treated at the end of the text. How can I put the content of the treated paste where the pointer is?

    
asked by anonymous 23.12.2016 / 13:16

2 answers

0

Apparently I managed to find a solution to the problem:

$(document).ready(function(){
	var clipboardData, pastedData;
	$(":input").on("paste", function (e) {
		e.preventDefault();
		clipboardData = e.clipboardData || window.clipboardData;
		pastedData = clipboardData.getData('text');
		var hist = pastedData;
		// smart single quotes and apostrophe
		pastedData = pastedData.replace(/[\u2018\u2019\u201A]/g, "\'");
		// smart double quotes
		pastedData = pastedData.replace(/[\u201C\u201D\u201E]/g, "\"");
		// ellipsis
		pastedData = pastedData.replace(/\u2026/g, "...");
		// dashes
		pastedData = pastedData.replace(/[\u2013\u2014]/g, "-");
		//alert(pastedData+" != "+hist);
		if (document.selection) {
			//For browsers like Internet Explorer
			this.focus();
			var sel = document.selection.createRange();
			sel.text = pastedData;
			this.focus();
		}else if (this.selectionStart || this.selectionStart == '0') {
			//For browsers like Firefox and Webkit based
			var startPos = this.selectionStart;
			var endPos = this.selectionEnd;
			var scrollTop = this.scrollTop;
			this.value = this.value.substring(0, startPos)+pastedData+this.value.substring(endPos,this.value.length);
			this.focus();
			this.selectionStart = startPos + pastedData.length;
			this.selectionEnd = startPos + pastedData.length;
			this.scrollTop = scrollTop;
		} else {
			this.value += pastedData;
			this.focus();
		}
	});
});
  

Source: link

    
23.12.2016 / 14:06
3

You will need to work with Selection Ranges .

Here, this function will insert the values from the position of the pointer:

function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA and others
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) +
            myValue +
            myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

Now replace this.value += pastedData; with insertAtCursor(this, pastedData); in your script.

  

Source:    link

    
23.12.2016 / 14:05