Implementing MaxLength for asp textbox

N

Not Me

Hi there,

There are hundreds of messages with different solutions on how to limit
the number of characters for a text box in ASP.net, none of which I can
get to work!!

I do lie, because using asp validators I can get a quick solution, but
I'd rather prevent the user from entering the characters in the first
place, rather than just telling them they've entered too much.

Many solutions tell me to use onkeydown etc. in the textbox parameters
- when I do this I get errors saying it's not allowed.. I seem to be
able to use attributes.add but nothing happens then. Also I'm being
told I need a 'type' parameter in my script, wheras all of the examples
just use 'language'..

here's my example code anyway, which is 'attached' to the textbox by
attributes.add.

<script type="text/jscript" language="jscript">
function LimitMultiLineLength(obj)
{
var iKey;
var eAny_Event = window.event;
iKey = eAny_Event.keyCode;
var re
re = new RegExp("\r\n","g")
x = obj.value.replace(re,"").length ;
if ((x >= obj.maxLength) && ((iKey > 33 && iKey <
255) || (iKey > 95 && iKey < 106)) && (iKey != 13))
{
if (obj.ErrorMessage )
{
alert(obj.ErrorMessage);
}
window.event.returnValue=false;
}
}
</script>

whereas nothing happens in IE, firefox is kind enough to tell me that
'LimitMultiLineLength is not defined' - so I'm guessing at least it's
being looked for.

any ideas? I thought there'd be a really simple solution to this!
(maybe there is...)

Cheers,
Chris
 
M

Mark Rae

any ideas? I thought there'd be a really simple solution to this!
(maybe there is...)

Er, well unless I'm missing something here...

<asp:TextBox ID="MyTextBox" runat="server" MaxLength="100" />
 
N

Not Me

Mark said:
Er, well unless I'm missing something here...

<asp:TextBox ID="MyTextBox" runat="server" MaxLength="100" />

D'oh, also forgot to mention, it's a multiline text box :p

cheers,
Chris
 
L

Laurent Bugnion, GalaSoft

Not said:
Hi there,

There are hundreds of messages with different solutions on how to limit
the number of characters for a text box in ASP.net, none of which I can
get to work!!

I do lie, because using asp validators I can get a quick solution, but
I'd rather prevent the user from entering the characters in the first
place, rather than just telling them they've entered too much.

Many solutions tell me to use onkeydown etc. in the textbox parameters
- when I do this I get errors saying it's not allowed.. I seem to be
able to use attributes.add but nothing happens then. Also I'm being
told I need a 'type' parameter in my script, wheras all of the examples
just use 'language'..

The "language" attribute on the "script" tag has been deprecated. The
type attribute replaces it. And unfortunately, most examples of
JavaScript on the web (I guess around 90% of them or more) are showing a
lot of bad practices. It's really terrible.

A good reference to learn JavaScript is David Flanagan's book
"JavaScript, the definitive guide", which was recently released in its
5th edition.

This should work on IE and Mozilla-based browsers:

<script type="text/javascript">
var MAX_LENGTH = 10;

function showLength( evt )
{
if ( window.event )
{
evt = window.event;
}

var taTest = null;

if ( evt.srcElement )
{
taTest = evt.srcElement;
}
if ( evt.target )
{
taTest = evt.target;
}
if ( !taTest )
{
throw "Unknown target";
}

var iLength = taTest.value.length;

if ( iLength > MAX_LENGTH )
{
taTest.value = taTest.value.substring( 0, MAX_LENGTH );
iLength = MAX_LENGTH;
}

var nSpanStatus = document.getElementById( "spanStatus" );
nSpanStatus.firstChild.nodeValue = iLength;

if ( iLength >= MAX_LENGTH )
{
nSpanStatus.style.color = "Red";
}
else
{
nSpanStatus.style.color = "Black";
}
}

function attachEvent()
{
var nTaTest = document.getElementById( "taTest" );
nTaTest.onkeyup = showLength;
}

</script>

with

<body onload="attachEvent();">

and

<form id="frmMain" action="HTMLPage1.htm">

<textarea id="taTest" rows="10" cols="20"
onkeyup="showLength();"></textarea>
<br /><br />
<span id="spanStatus" style=color: Black;">&nbsp;</span>

</form>

HTH,
Laurent
 
N

Not Me

This should work on IE and Mozilla-based browsers:

<script type="text/javascript">
var MAX_LENGTH = 10;

function showLength( evt )
{
if ( window.event )
{
evt = window.event;
}

var taTest = null;

if ( evt.srcElement )
{
taTest = evt.srcElement;
}
if ( evt.target )
{
taTest = evt.target;
}
if ( !taTest )
{
throw "Unknown target";
}

var iLength = taTest.value.length;

if ( iLength > MAX_LENGTH )
{
taTest.value = taTest.value.substring( 0, MAX_LENGTH );
iLength = MAX_LENGTH;
}

var nSpanStatus = document.getElementById( "spanStatus" );
nSpanStatus.firstChild.nodeValue = iLength;

if ( iLength >= MAX_LENGTH )
{
nSpanStatus.style.color = "Red";
}
else
{
nSpanStatus.style.color = "Black";
}
}

function attachEvent()
{
var nTaTest = document.getElementById( "taTest" );
nTaTest.onkeyup = showLength;
}

</script>

with

<body onload="attachEvent();">

and

<form id="frmMain" action="HTMLPage1.htm">

<textarea id="taTest" rows="10" cols="20"
onkeyup="showLength();"></textarea>
<br /><br />
<span id="spanStatus" style=color: Black;">&nbsp;</span>

</form>

HTH,
Laurent

Thank you very much Laurent, works perfectly!! :)

cheers,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top