|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Javascipt: counting chars!
I have amended a javascript function i found on the web to count charecters within a textbox as i write in it. This works...
Code = Code:
<html>
<head>
<title></title>
<script type="text/javascript" >
function CheckFieldLength(fn,wn,rn,mc) {
var len = fn.value.length;
if (len > mc) {
fn.value = fn.value.substring(0,mc);
len = mc;
}
document.getElementById(wn).innerHTML = len;
}
</script>
</head>
<body bgcolor="#ffffff" >
<form name=form1 method=post action='http://www.plus2net.com'>
<textarea name="product_profile" cols="120" rows="15" class="formelement" id="taMessage" onkeyup="CheckFieldLength(taMessage, 'charcount', 'remaining', 10000);" onkeydown="CheckFieldLength(taMessage, 'charcount', 'remaining', 10000);" onmouseout="CheckFieldLength(taMessage, 'charcount', 'remaining', 10000);"></textarea>
<strong>Contains <span id="charcount" class="compulsory">0</span> characters entered. </strong></p>
</form>
</body>
</html>
The problem i have is i don't want this counter to include spaces. Thanks in advance Can anyone help? Edit/Delete Message |
|
#2
|
|||
|
|||
|
Code:
<html>
<head>
<title></title>
<script type="text/javascript" >
function CheckFieldLength(fn,wn,rn,mc) {
var len = fn.value.length;
if (len > mc) {
fn.value = fn.value.substring(0,mc);
len = mc;
}
var spaces = 0
//window.alert(document.getElementById('taMessage').value.split(' ').length )
if (document.getElementById('taMessage').value.split(' ').length > 1 )
{
spaces = document.getElementById('taMessage').value.split(' ').length;
spaces = spaces - 1
}
else
{
spaces = 0
}
document.getElementById(wn).innerHTML = len - spaces;
//window.alert(len)
}
</script>
</head>
<body bgcolor="#ffffff" >
<form name="form1" method=post action='http://www.plus2net.com'>
<textarea name="product_profile" cols="120" rows="15" class="formelement" id="taMessage" onkeyup="CheckFieldLength(taMessage, 'charcount', 'remaining', 10000);" onkeydown="CheckFieldLength(taMessage, 'charcount', 'remaining', 10000);" onmouseout="CheckFieldLength(taMessage, 'charcount', 'remaining', 10000);"></textarea>
<strong>Contains <span id="charcount" class="compulsory">0</span> characters entered. </strong></p>
</form>
</body>
</html>
|
|
#3
|
||||
|
||||
|
A simpler change than the one posted by whipaus follows. I'm not addressing any of the flaws, such as the fact that adding text in the middle will snip text off from the end.
Code:
function CheckFieldLength(fn,wn,rn,mc) {
var len = fn.value.replace(/\s/g, '').length;
if (len > mc) {
fn.value = fn.value.substring(0,mc);
len = mc;
}
document.getElementById(wn).innerHTML = len;
}
|
|
#4
|
|||||
|
|||||
|
a little more direct
makes the browser work less as it doesn't have to perform a regular expression search on every keystroke.
html Code:
Edit: Increased the efficiency by adding 'charcount' global. (eliminates the need to fetch and parse the html on every keystroke) Last edited by ktoz : May 5th, 2008 at 07:49 AM. |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > Javascipt: counting chars! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|