STEP-->1
add this javascript function in aspx page
<script language="javascript" type="text/javascript">
function validateLimit(obj, divID, maxchar) {
objDiv = get_object(divID);
if (this.id) obj = this;
var remaningChar = maxchar - trimEnter(obj.value).length;
if (objDiv.id) {
objDiv.innerHTML = remaningChar + " characters left";
}
if (remaningChar <= 0) {
obj.value = obj.value.substring(maxchar, 0);
if (objDiv.id) {
objDiv.innerHTML = "0 characters left";
}
return false;
}
else
{ return true; }
}
function get_object(id) {
var object = null;
if (document.layers) {
object = document.layers[id];
} else if (document.all) {
object = document.all[id];
} else if (document.getElementById) {
object = document.getElementById(id);
}
return object;
}
function trimEnter(dataStr) {
return dataStr.replace(/(\r\n|\r|\n)/g, "");
}
</script>
STEP--> 2
assign javascript function to onkeyup event
<asp:TextBox ID="yourtestboxid" runat="server" onkeyup="return validateLimit(this, 'lblMsg1', 50)"></asp:TextBox>
<div id="lblMsg1"> 50 characters left</div>==>this is displaying remaining character in aspx page
add this javascript function in aspx page
<script language="javascript" type="text/javascript">
function validateLimit(obj, divID, maxchar) {
objDiv = get_object(divID);
if (this.id) obj = this;
var remaningChar = maxchar - trimEnter(obj.value).length;
if (objDiv.id) {
objDiv.innerHTML = remaningChar + " characters left";
}
if (remaningChar <= 0) {
obj.value = obj.value.substring(maxchar, 0);
if (objDiv.id) {
objDiv.innerHTML = "0 characters left";
}
return false;
}
else
{ return true; }
}
function get_object(id) {
var object = null;
if (document.layers) {
object = document.layers[id];
} else if (document.all) {
object = document.all[id];
} else if (document.getElementById) {
object = document.getElementById(id);
}
return object;
}
function trimEnter(dataStr) {
return dataStr.replace(/(\r\n|\r|\n)/g, "");
}
</script>
STEP--> 2
assign javascript function to onkeyup event
<asp:TextBox ID="yourtestboxid" runat="server" onkeyup="return validateLimit(this, 'lblMsg1', 50)"></asp:TextBox>
<div id="lblMsg1"> 50 characters left</div>==>this is displaying remaining character in aspx page
No comments:
Post a Comment