<label id="charsRemain">20</label>
<asp:HiddenField ID="hfMaxCharacter" runat="server" Value="20" />
<asp:TextBox ID="txtSmsMsg" runat="server" Rows="5" Columns="50" TextMode="MultiLine" onkeyup="myFunctionChars()"></asp:TextBox>
function myFunctionChars() {
var txt = document.getElementById('<%=txtSmsMsg.ClientID %>').value;
var max = document.getElementById('<%=hfMaxCharacter.ClientID %>').value;
var leng = txt.length;
var eachLine = txt.split('\n');
leng += (eachLine.length - 1)
if (leng <= max) {
//var totalChars = $("#<%= this.hfMaxCharacter.ClientID %>").val() - leng;
document.getElementById('charsRemain').innerHTML = max - leng;
}
else {
var i = 0;
var newTxt = txt.substr(0, max);
var newLen;
var originalLen;
//alert(newTxt);
//alert(newLen);
while (i == 0) {
originalLen = newTxt.length;
newLen = newTxt.length;
//alert(newLen + " - 1")
var eachLine2 = newTxt.split('\n');
newLen += (eachLine2.length - 1);
//alert(newLen + " - 2")
if (newLen <= max) {
//alert("i = 1");
i = 1;
}
else {
//alert(newLen + " - 3");
newTxt = newTxt.substr(0, originalLen - 1);
//alert(newTxt.length + " - 4");
i = 0;
}
document.getElementById('charsRemain').innerHTML = max - newLen;
//alert(max + ' - ' + newLen)
document.getElementById('<%=txtSmsMsg.ClientID %>').value = newTxt;
}
}
}