Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2012, 01:23 PM   PM User | #31
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
I am sorry that I created such trouble over this topic.
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-14-2012, 01:30 PM   PM User | #32
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
Quote:
Originally Posted by Philip M View Post
USE THIS!!!

Code:
<html>
<head>

<script type = "text/javascript">

var maxLen = 1000; // max number of characters allowed  // global variable
var max = "You may enter up to " + maxLen + " characters";  // global variable

function OnPaste () {
//return false;   // cancels (blocks)  the onpaste event.  Uncomment this line to block pasting
setTimeout(checkMaxInput,100);  // delay is necessary
}

function initCount() {
document.getElementById("limit").innerHTML = max; 
document.getElementById("remLen").value = maxLen;
}

function checkMaxInput() {
var form = document.myform;  // or document.forms[0] or document.getElementById("myform");
if (form.txtarea.value.length > maxLen) {   // if too long.... trim it!
form.txtarea.value = form.txtarea.value.substring(0, maxLen);
document.getElementById("message").innerHTML = "Too many characters were entered!!  The excess over " + maxLen + " have been removed.";
}
else {
document.getElementById("message").innerHTML = "";
}
form.remLen.value = maxLen - form.txtarea.value.length;
}

</script>

</head>

<body onload = "initCount()">

<form name = "myform" id = "myform">
<span id = "limit"  style="font-size: 10pt;color: #FF0000;font-family: arial, helvetica, sans-serif;"></span>
<br>
<textarea name="txtarea" wrap=physical cols=48 rows=10  onkeyup="checkMaxInput()"  onblur="checkMaxInput()" onpaste="OnPaste ()" >
</textarea>
<br>
<input readonly type=text name=remLen id = "remLen" size=3 value = ""> characters left</font>
</form>
<span id = "message" style="color:red";></span>
</body>
</html>
Ran into a problem, if you try to type more at the end of it, it doesn't allow it, however if you put one up in say the middle, it allows it. Such as those 3's. It doesn't post after 222222222, but above it does.

But I could just do a character check after submitting. What about trimming the post on submit? so any characters over 1,000 will get removed before it enters the database?
Attached Thumbnails
Click image for larger version

Name:	ad.jpg
Views:	9
Size:	36.0 KB
ID:	11051  
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-14-2012, 03:47 PM   PM User | #33
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by myfayt View Post
Ran into a problem, if you try to type more at the end of it, it doesn't allow it, however if you put one up in say the middle, it allows it. Such as those 3's. It doesn't post after 222222222, but above it does.
Sorry, I do not understand what you are driving at.

If you reach the limit of 1000 characters (whether by typing or pasting) then no more characters can be added to the textarea. If you try to add another character at the end it will not allow it. If you add a character in the middle of the text (which amounts to an edit) then one character is removed from the end to compensate, still leaving 1000. OK?

There is no need to repeat the validation onsubmit. But of course the user can always disable Javascript so you must repeat the validation on the server.

I do hope we have reached the end of this topic now!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 04-14-2012 at 04:02 PM.. Reason: Noticed typo
Philip M is offline   Reply With Quote
Old 04-14-2012, 04:17 PM   PM User | #34
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
After more testing, you are correct, it's working perfectly. Thank you and everyone else so much!
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 AM.


Advertisement
Log in to turn off these ads.