Monday, January 2, 2012

Limit no of character in textbox with count in Asp.net using javascript


html page:
=======
<script type="text/javascript">
function textCounter(field,maxlimit)
{
if (field.value.length > maxlimit)
{
return false;
}
}
</script>
Page Load Events:
=============
protected void Page_Load(object sender, EventArgs e)
{
txtmaxlen.Attributes.Add("onkeydown", "return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onkeyup", "return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onmousedown","return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onmouseup","return textCounter(this, 5)");
txtmaxlen.Attributes.Add("onblur", "return textCounter(this, 5)");
}



1 comment:

  1. script type="text/javascript"
    function textCounter(field, maxlimit) {

    if (field.value.length maxlimit)
    field.value = field.value.substring(0, maxlimit); // maxLength);
    {
    return true;
    }
    }
    /script

    ReplyDelete