Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interpreting keystrokes

Status
Not open for further replies.

Haraldo

Programmer
Jun 9, 2003
41
GB
I would like to create a character count for a textarea which counts the number of characters but when a new line (return is pressed) the counter defaults back to a chosen number such as 160 or whatever.

So far i have been able to get the counter to increase and decrease on keystrokes but am unable to identify the return character. How do i check for a return chatacter so i can reset the counter and start again on a new line.

// So far
<script language="Javascript">

var max_len = <?= $maxPIN ?>;
var old_length = 0

// spn = span tag - field = value
function updateMaxPin(spn, field)
{
var string_len = field.value.length;

//alert(field.value+' '+string_len)

if ( string_len > 0 )
{
if ( string_len > old_length )
{
max_len = max_len - 1
}
else if ( string_len < old_length )
{
max_len = max_len + 1
}

old_length = string_len
}

if ( string_len == 0 )
{
spn.innerText = max_len+1
}
else
{
spn.innerText = max_len
}
}

</script>

Thanks for any help,

HB
 
When I escape(...) the value in a textarea using IE6, I notice that it interprets new line characters as '%0D%0A' (those are zeros, not o's).

So, if escape(...) works the same everywhere, you can try:
Code:
 var dfav = document.forms[0].textA.value;
 var nl = '%0D%0A';
 var val = escape(dfav);
 if(val.length == val.lastIndexOf(nl)+(nl).length)
 {
  ...
 }//end if

Try alert(val) after the val declaration to see what the 'escape(...)' command does to the textarea value.

'hope that helps.

--Dave
 
or u could simply replcae the new line characters.

TheString = field.value.replace(/\r\n/g,"")
var string_len = TheString.length;


Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top