Limit the number of characters in a Text Field
by Radhika • March 9, 2010 • MOSS 2007 • 1 Comment
In Moss 2007 we can limit the number of characters in a text filed. But if you want alert the user when the limit is exceeded then exceeding the characters length then we can use this script .
1. Go to Site Actions -> Edit Page.
Note: If the Edit option is disabled then you can go to edit mode by appending the following text to the url.
?pageview=shared &toolpaneview=2
3. Add a Content Editor Web Part below that web part that has the text box. Add this java script in it.
<script type="text/java script">
var content=document.getElementById('id of the Text Box'); //You can get the ID of the Text Box by vieweing the Page Source.
content.onkeyup=function()
{
setlength(this,10) //Text length is limited to 10 chars
};
function setlength(obj,maxlen)
{
if(this.id) obj = this;
var remaningChar = maxlen - obj.value.length;
if( remaningChar <= 0)
{
alert('Character Limit exceeds!');
return false;
}
else
{return true;}
}
</script>

Hii, I tried this code but its not working for me. I copied Text Box id from Page Source. I have added this code to content editor webpart. Though its not giving any javascript error its not working as expected.
Thanks