replace last character input into content editable div (tried all options)
i have a message box defined by a content editable div. The user can edit their message after input, and i have a character countdown , but when i get to 0 characters left, i cannot for the life of me find a suitable method to remove the last character or disallow the final keypress
The current method simply sends the cursor back to the start of the message and does not remove the character. Iv tried almost every suggestion on the internet from splice to regex to substring. i have found none of them to work.
The code i am showing you below is my own code and has the same problem, at 0 the cursor simply goes back to the start of the message and then allows many more characters again without limiting the amount of characters allowed...
Could anybody help me out here and suggest a method to achieve this simple goal. i would be very great-full.
Here is my code.
$(function () {
$(ebm).keydown(checklimitsBuyer);
});
function checklimitsBuyer(){
var username = $(ebm).html();
var nameReg = /^.{0,100}$/;
var messlength = username.length;
var amount=100;
var leftlength=amount-messlength;
var ebmname=document.getElementById('Bname'+varmessageid+'');
if (messlength < amount) {
$(ebmname).text('Chars Left'+leftlength);
}
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
//alert('Only 300 Characters Allowed');
var newStr = username.replace(/.$/,".")
$(ebm).text(newStr);
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
}
html keydown jscript
add a comment |
i have a message box defined by a content editable div. The user can edit their message after input, and i have a character countdown , but when i get to 0 characters left, i cannot for the life of me find a suitable method to remove the last character or disallow the final keypress
The current method simply sends the cursor back to the start of the message and does not remove the character. Iv tried almost every suggestion on the internet from splice to regex to substring. i have found none of them to work.
The code i am showing you below is my own code and has the same problem, at 0 the cursor simply goes back to the start of the message and then allows many more characters again without limiting the amount of characters allowed...
Could anybody help me out here and suggest a method to achieve this simple goal. i would be very great-full.
Here is my code.
$(function () {
$(ebm).keydown(checklimitsBuyer);
});
function checklimitsBuyer(){
var username = $(ebm).html();
var nameReg = /^.{0,100}$/;
var messlength = username.length;
var amount=100;
var leftlength=amount-messlength;
var ebmname=document.getElementById('Bname'+varmessageid+'');
if (messlength < amount) {
$(ebmname).text('Chars Left'+leftlength);
}
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
//alert('Only 300 Characters Allowed');
var newStr = username.replace(/.$/,".")
$(ebm).text(newStr);
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
}
html keydown jscript
add a comment |
i have a message box defined by a content editable div. The user can edit their message after input, and i have a character countdown , but when i get to 0 characters left, i cannot for the life of me find a suitable method to remove the last character or disallow the final keypress
The current method simply sends the cursor back to the start of the message and does not remove the character. Iv tried almost every suggestion on the internet from splice to regex to substring. i have found none of them to work.
The code i am showing you below is my own code and has the same problem, at 0 the cursor simply goes back to the start of the message and then allows many more characters again without limiting the amount of characters allowed...
Could anybody help me out here and suggest a method to achieve this simple goal. i would be very great-full.
Here is my code.
$(function () {
$(ebm).keydown(checklimitsBuyer);
});
function checklimitsBuyer(){
var username = $(ebm).html();
var nameReg = /^.{0,100}$/;
var messlength = username.length;
var amount=100;
var leftlength=amount-messlength;
var ebmname=document.getElementById('Bname'+varmessageid+'');
if (messlength < amount) {
$(ebmname).text('Chars Left'+leftlength);
}
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
//alert('Only 300 Characters Allowed');
var newStr = username.replace(/.$/,".")
$(ebm).text(newStr);
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
}
html keydown jscript
i have a message box defined by a content editable div. The user can edit their message after input, and i have a character countdown , but when i get to 0 characters left, i cannot for the life of me find a suitable method to remove the last character or disallow the final keypress
The current method simply sends the cursor back to the start of the message and does not remove the character. Iv tried almost every suggestion on the internet from splice to regex to substring. i have found none of them to work.
The code i am showing you below is my own code and has the same problem, at 0 the cursor simply goes back to the start of the message and then allows many more characters again without limiting the amount of characters allowed...
Could anybody help me out here and suggest a method to achieve this simple goal. i would be very great-full.
Here is my code.
$(function () {
$(ebm).keydown(checklimitsBuyer);
});
function checklimitsBuyer(){
var username = $(ebm).html();
var nameReg = /^.{0,100}$/;
var messlength = username.length;
var amount=100;
var leftlength=amount-messlength;
var ebmname=document.getElementById('Bname'+varmessageid+'');
if (messlength < amount) {
$(ebmname).text('Chars Left'+leftlength);
}
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
//alert('Only 300 Characters Allowed');
var newStr = username.replace(/.$/,".")
$(ebm).text(newStr);
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
}
html keydown jscript
html keydown jscript
asked Nov 15 '18 at 2:03
lezzlezz
256
256
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If you want to limit the number of characters the user can input, you can try something like this:
$(window).ready(()=>{
// our max number of characters
let max = 10;
// set the text of the div that allows the user to
// keep track of how many characters he can still input
$('#char-count').text(`remaining characters: ${max}`);
$('#message').on('keypress input paste', function(e){
// the number of characters in our content editable div
let charCount = $(this).text().length;
if(charCount >= max){
// prevent the user from typing
e.preventDefault();
}
$('#char-count').text(`remaining characters: ${max - charCount}`);
});
});#message {
border: 1px dashed green;
height: 100px;
}
#char-count { :
margin-top: 10px;
}<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<div id="message" contenteditable="true"></div>
<div id="char-count"></div>Also, here's a working example :)
Edit
- Added support for preventing copy and pasting when the limit is reached.
var ebm=document.getElementById('editBuyer'+varmessageid+''); $(ebm).on('keypress', function(e){ var username = $(ebm).html(); var nameReg = /^.{0,300}$/; if(!nameReg.test(username)) { $(ebm).css('border', '1px dashed red'); e.preventDefault(); alert('Only 300 Characters Allowed'); } if(nameReg.test(username)) { $(ebm).css('border', '1px dashed #07f310'); } });
– lezz
Nov 15 '18 at 3:25
@lezz, I've updated my answer. Also, don't forget to vote and mark this as answer if it solves your problem :)
– C.RaysOfTheSun
Nov 15 '18 at 3:32
im sorry if this causes any problems for you but i hit the flag thing....By accident...
– lezz
Nov 15 '18 at 5:01
Nah. All’s good. Happy coding! 😊
– C.RaysOfTheSun
Nov 15 '18 at 6:59
add a comment |
Thank you v.much
var ebm=document.getElementById('editBuyer'+varmessageid+'');
$(ebm).on('keypress', function(e){
var username = $(ebm).html();
var nameReg = /^.{0,300}$/;
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
e.preventDefault();
alert('Only 300 Characters Allowed');
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
});
keypress helped alot also, had issues with keyup and keydown as count was not taken into consideration until after the event. whereas keypress somehow made the count with no problems..
– lezz
Nov 15 '18 at 4:52
only problem i have now, is that a delete or backspace is not counted as a -minus in the count, i get the count after a asci keypress only..
– lezz
Nov 15 '18 at 4:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311393%2freplace-last-character-input-into-content-editable-div-tried-all-options%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to limit the number of characters the user can input, you can try something like this:
$(window).ready(()=>{
// our max number of characters
let max = 10;
// set the text of the div that allows the user to
// keep track of how many characters he can still input
$('#char-count').text(`remaining characters: ${max}`);
$('#message').on('keypress input paste', function(e){
// the number of characters in our content editable div
let charCount = $(this).text().length;
if(charCount >= max){
// prevent the user from typing
e.preventDefault();
}
$('#char-count').text(`remaining characters: ${max - charCount}`);
});
});#message {
border: 1px dashed green;
height: 100px;
}
#char-count { :
margin-top: 10px;
}<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<div id="message" contenteditable="true"></div>
<div id="char-count"></div>Also, here's a working example :)
Edit
- Added support for preventing copy and pasting when the limit is reached.
var ebm=document.getElementById('editBuyer'+varmessageid+''); $(ebm).on('keypress', function(e){ var username = $(ebm).html(); var nameReg = /^.{0,300}$/; if(!nameReg.test(username)) { $(ebm).css('border', '1px dashed red'); e.preventDefault(); alert('Only 300 Characters Allowed'); } if(nameReg.test(username)) { $(ebm).css('border', '1px dashed #07f310'); } });
– lezz
Nov 15 '18 at 3:25
@lezz, I've updated my answer. Also, don't forget to vote and mark this as answer if it solves your problem :)
– C.RaysOfTheSun
Nov 15 '18 at 3:32
im sorry if this causes any problems for you but i hit the flag thing....By accident...
– lezz
Nov 15 '18 at 5:01
Nah. All’s good. Happy coding! 😊
– C.RaysOfTheSun
Nov 15 '18 at 6:59
add a comment |
If you want to limit the number of characters the user can input, you can try something like this:
$(window).ready(()=>{
// our max number of characters
let max = 10;
// set the text of the div that allows the user to
// keep track of how many characters he can still input
$('#char-count').text(`remaining characters: ${max}`);
$('#message').on('keypress input paste', function(e){
// the number of characters in our content editable div
let charCount = $(this).text().length;
if(charCount >= max){
// prevent the user from typing
e.preventDefault();
}
$('#char-count').text(`remaining characters: ${max - charCount}`);
});
});#message {
border: 1px dashed green;
height: 100px;
}
#char-count { :
margin-top: 10px;
}<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<div id="message" contenteditable="true"></div>
<div id="char-count"></div>Also, here's a working example :)
Edit
- Added support for preventing copy and pasting when the limit is reached.
var ebm=document.getElementById('editBuyer'+varmessageid+''); $(ebm).on('keypress', function(e){ var username = $(ebm).html(); var nameReg = /^.{0,300}$/; if(!nameReg.test(username)) { $(ebm).css('border', '1px dashed red'); e.preventDefault(); alert('Only 300 Characters Allowed'); } if(nameReg.test(username)) { $(ebm).css('border', '1px dashed #07f310'); } });
– lezz
Nov 15 '18 at 3:25
@lezz, I've updated my answer. Also, don't forget to vote and mark this as answer if it solves your problem :)
– C.RaysOfTheSun
Nov 15 '18 at 3:32
im sorry if this causes any problems for you but i hit the flag thing....By accident...
– lezz
Nov 15 '18 at 5:01
Nah. All’s good. Happy coding! 😊
– C.RaysOfTheSun
Nov 15 '18 at 6:59
add a comment |
If you want to limit the number of characters the user can input, you can try something like this:
$(window).ready(()=>{
// our max number of characters
let max = 10;
// set the text of the div that allows the user to
// keep track of how many characters he can still input
$('#char-count').text(`remaining characters: ${max}`);
$('#message').on('keypress input paste', function(e){
// the number of characters in our content editable div
let charCount = $(this).text().length;
if(charCount >= max){
// prevent the user from typing
e.preventDefault();
}
$('#char-count').text(`remaining characters: ${max - charCount}`);
});
});#message {
border: 1px dashed green;
height: 100px;
}
#char-count { :
margin-top: 10px;
}<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<div id="message" contenteditable="true"></div>
<div id="char-count"></div>Also, here's a working example :)
Edit
- Added support for preventing copy and pasting when the limit is reached.
If you want to limit the number of characters the user can input, you can try something like this:
$(window).ready(()=>{
// our max number of characters
let max = 10;
// set the text of the div that allows the user to
// keep track of how many characters he can still input
$('#char-count').text(`remaining characters: ${max}`);
$('#message').on('keypress input paste', function(e){
// the number of characters in our content editable div
let charCount = $(this).text().length;
if(charCount >= max){
// prevent the user from typing
e.preventDefault();
}
$('#char-count').text(`remaining characters: ${max - charCount}`);
});
});#message {
border: 1px dashed green;
height: 100px;
}
#char-count { :
margin-top: 10px;
}<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<div id="message" contenteditable="true"></div>
<div id="char-count"></div>Also, here's a working example :)
Edit
- Added support for preventing copy and pasting when the limit is reached.
$(window).ready(()=>{
// our max number of characters
let max = 10;
// set the text of the div that allows the user to
// keep track of how many characters he can still input
$('#char-count').text(`remaining characters: ${max}`);
$('#message').on('keypress input paste', function(e){
// the number of characters in our content editable div
let charCount = $(this).text().length;
if(charCount >= max){
// prevent the user from typing
e.preventDefault();
}
$('#char-count').text(`remaining characters: ${max - charCount}`);
});
});#message {
border: 1px dashed green;
height: 100px;
}
#char-count { :
margin-top: 10px;
}<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<div id="message" contenteditable="true"></div>
<div id="char-count"></div>$(window).ready(()=>{
// our max number of characters
let max = 10;
// set the text of the div that allows the user to
// keep track of how many characters he can still input
$('#char-count').text(`remaining characters: ${max}`);
$('#message').on('keypress input paste', function(e){
// the number of characters in our content editable div
let charCount = $(this).text().length;
if(charCount >= max){
// prevent the user from typing
e.preventDefault();
}
$('#char-count').text(`remaining characters: ${max - charCount}`);
});
});#message {
border: 1px dashed green;
height: 100px;
}
#char-count { :
margin-top: 10px;
}<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<div id="message" contenteditable="true"></div>
<div id="char-count"></div>edited Nov 15 '18 at 3:31
answered Nov 15 '18 at 3:10
C.RaysOfTheSunC.RaysOfTheSun
655127
655127
var ebm=document.getElementById('editBuyer'+varmessageid+''); $(ebm).on('keypress', function(e){ var username = $(ebm).html(); var nameReg = /^.{0,300}$/; if(!nameReg.test(username)) { $(ebm).css('border', '1px dashed red'); e.preventDefault(); alert('Only 300 Characters Allowed'); } if(nameReg.test(username)) { $(ebm).css('border', '1px dashed #07f310'); } });
– lezz
Nov 15 '18 at 3:25
@lezz, I've updated my answer. Also, don't forget to vote and mark this as answer if it solves your problem :)
– C.RaysOfTheSun
Nov 15 '18 at 3:32
im sorry if this causes any problems for you but i hit the flag thing....By accident...
– lezz
Nov 15 '18 at 5:01
Nah. All’s good. Happy coding! 😊
– C.RaysOfTheSun
Nov 15 '18 at 6:59
add a comment |
var ebm=document.getElementById('editBuyer'+varmessageid+''); $(ebm).on('keypress', function(e){ var username = $(ebm).html(); var nameReg = /^.{0,300}$/; if(!nameReg.test(username)) { $(ebm).css('border', '1px dashed red'); e.preventDefault(); alert('Only 300 Characters Allowed'); } if(nameReg.test(username)) { $(ebm).css('border', '1px dashed #07f310'); } });
– lezz
Nov 15 '18 at 3:25
@lezz, I've updated my answer. Also, don't forget to vote and mark this as answer if it solves your problem :)
– C.RaysOfTheSun
Nov 15 '18 at 3:32
im sorry if this causes any problems for you but i hit the flag thing....By accident...
– lezz
Nov 15 '18 at 5:01
Nah. All’s good. Happy coding! 😊
– C.RaysOfTheSun
Nov 15 '18 at 6:59
var ebm=document.getElementById('editBuyer'+varmessageid+''); $(ebm).on('keypress', function(e){ var username = $(ebm).html(); var nameReg = /^.{0,300}$/; if(!nameReg.test(username)) { $(ebm).css('border', '1px dashed red'); e.preventDefault(); alert('Only 300 Characters Allowed'); } if(nameReg.test(username)) { $(ebm).css('border', '1px dashed #07f310'); } });
– lezz
Nov 15 '18 at 3:25
var ebm=document.getElementById('editBuyer'+varmessageid+''); $(ebm).on('keypress', function(e){ var username = $(ebm).html(); var nameReg = /^.{0,300}$/; if(!nameReg.test(username)) { $(ebm).css('border', '1px dashed red'); e.preventDefault(); alert('Only 300 Characters Allowed'); } if(nameReg.test(username)) { $(ebm).css('border', '1px dashed #07f310'); } });
– lezz
Nov 15 '18 at 3:25
@lezz, I've updated my answer. Also, don't forget to vote and mark this as answer if it solves your problem :)
– C.RaysOfTheSun
Nov 15 '18 at 3:32
@lezz, I've updated my answer. Also, don't forget to vote and mark this as answer if it solves your problem :)
– C.RaysOfTheSun
Nov 15 '18 at 3:32
im sorry if this causes any problems for you but i hit the flag thing....By accident...
– lezz
Nov 15 '18 at 5:01
im sorry if this causes any problems for you but i hit the flag thing....By accident...
– lezz
Nov 15 '18 at 5:01
Nah. All’s good. Happy coding! 😊
– C.RaysOfTheSun
Nov 15 '18 at 6:59
Nah. All’s good. Happy coding! 😊
– C.RaysOfTheSun
Nov 15 '18 at 6:59
add a comment |
Thank you v.much
var ebm=document.getElementById('editBuyer'+varmessageid+'');
$(ebm).on('keypress', function(e){
var username = $(ebm).html();
var nameReg = /^.{0,300}$/;
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
e.preventDefault();
alert('Only 300 Characters Allowed');
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
});
keypress helped alot also, had issues with keyup and keydown as count was not taken into consideration until after the event. whereas keypress somehow made the count with no problems..
– lezz
Nov 15 '18 at 4:52
only problem i have now, is that a delete or backspace is not counted as a -minus in the count, i get the count after a asci keypress only..
– lezz
Nov 15 '18 at 4:53
add a comment |
Thank you v.much
var ebm=document.getElementById('editBuyer'+varmessageid+'');
$(ebm).on('keypress', function(e){
var username = $(ebm).html();
var nameReg = /^.{0,300}$/;
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
e.preventDefault();
alert('Only 300 Characters Allowed');
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
});
keypress helped alot also, had issues with keyup and keydown as count was not taken into consideration until after the event. whereas keypress somehow made the count with no problems..
– lezz
Nov 15 '18 at 4:52
only problem i have now, is that a delete or backspace is not counted as a -minus in the count, i get the count after a asci keypress only..
– lezz
Nov 15 '18 at 4:53
add a comment |
Thank you v.much
var ebm=document.getElementById('editBuyer'+varmessageid+'');
$(ebm).on('keypress', function(e){
var username = $(ebm).html();
var nameReg = /^.{0,300}$/;
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
e.preventDefault();
alert('Only 300 Characters Allowed');
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
});
Thank you v.much
var ebm=document.getElementById('editBuyer'+varmessageid+'');
$(ebm).on('keypress', function(e){
var username = $(ebm).html();
var nameReg = /^.{0,300}$/;
if(!nameReg.test(username)) {
$(ebm).css('border', '1px dashed red');
e.preventDefault();
alert('Only 300 Characters Allowed');
}
if(nameReg.test(username)) {
$(ebm).css('border', '1px dashed #07f310');
}
});
answered Nov 15 '18 at 3:24
lezzlezz
256
256
keypress helped alot also, had issues with keyup and keydown as count was not taken into consideration until after the event. whereas keypress somehow made the count with no problems..
– lezz
Nov 15 '18 at 4:52
only problem i have now, is that a delete or backspace is not counted as a -minus in the count, i get the count after a asci keypress only..
– lezz
Nov 15 '18 at 4:53
add a comment |
keypress helped alot also, had issues with keyup and keydown as count was not taken into consideration until after the event. whereas keypress somehow made the count with no problems..
– lezz
Nov 15 '18 at 4:52
only problem i have now, is that a delete or backspace is not counted as a -minus in the count, i get the count after a asci keypress only..
– lezz
Nov 15 '18 at 4:53
keypress helped alot also, had issues with keyup and keydown as count was not taken into consideration until after the event. whereas keypress somehow made the count with no problems..
– lezz
Nov 15 '18 at 4:52
keypress helped alot also, had issues with keyup and keydown as count was not taken into consideration until after the event. whereas keypress somehow made the count with no problems..
– lezz
Nov 15 '18 at 4:52
only problem i have now, is that a delete or backspace is not counted as a -minus in the count, i get the count after a asci keypress only..
– lezz
Nov 15 '18 at 4:53
only problem i have now, is that a delete or backspace is not counted as a -minus in the count, i get the count after a asci keypress only..
– lezz
Nov 15 '18 at 4:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311393%2freplace-last-character-input-into-content-editable-div-tried-all-options%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown