Friday, October 21, 2016

Asp.Net: jQuery function Validate MaxLength of TextMode="MultiLine" of textbox

I am discussing, How to validate the "MaxLength" of "TextBox" or whose TextMode="MultiLine"?

Here I am using below jQuery function for validating the max-length of the multiline textbox. The code is as below

jQuery function:

function CalculateMaxLength(ControlID, length) {
var obj = $('#' + ControlID);
var strLength = obj.val().length;
if (strLength > length) {
obj.val(obj.val().substring(0, length));
//alert('Only '+length+' character allowed.');

Above function call as below:
<!--Comments -->
<asp:Label ID="Label33" runat="server" Text="Comments"></asp:Label>
<asp:TextBox ID="txtComments" runat="server" Rows="5" TextMode="MultiLine"
onkeyup="return CalculateMaxLength('txtComments',200);">

In the above function, you can use it for all Multiline textboxes you used in your application. Here you pass the Control-Id and the required max-length of the textbox. This validation deletes all strings onkeyup all characters after defining max-length. And please verify that the ClientIDMode="Static" is mandatory.

No comments:

Post a Comment