Saturday, 2 November 2013

maximum length validation of multilinetextbox in asp.net


solution1:

Using Javascript in asp.net

function Count(text,long)

{

      var maxlength = new Number(long);

if(document.getElementById('<%=textBox.ClientID%>').value.length > maxlength){

            text.value = text.value.substring(0,maxlength);

            alert(" Only " + long + " chars");
}

Your textbox code should look like

<asp:TextBox ID="textBox" onKeyUp="javascript:Count(this,200);" onChange="javascript:Count(this,200);"  TextMode=MultiLine Columns="5" Rows="5" runat=server>
    </asp:TextBox>


solution2:

using regularexpression in asp.net

<asp:RegularExpressionValidator ID="txtConclusionValidator1" ControlToValidate="textBox" Text="Exceeding 200 characters" ValidationExpression="^[\s\S]{0,2}$" runat="server" />

<asp:TextBox ID="textBox" TextMode=MultiLine Columns="5" Rows="5" runat=server></asp:TextBox>


solution3:

using jqury in asp.net

$(document).ready(function () {
        var maxLength = 150;
        var txt = $('#<%=lbladdress.ClientID%>');
        txt.html('');
        $('#<%=txt_HoAddress.ClientID%>').keyup(function () {
            var text = $(this).val();
            if (text != "") {
                var textLength = text.length;
                if (textLength > maxLength) {
                    $(this).val(text.substring(0, (maxLength)));
                    txt.html("Sorry, you only " + maxLength + " characters are allowed");
                    text.focus();
                }
            }
            else {
                txt.html("Please Enter the Address");
            }
        });
    });

or

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
   $(function () {
            var limit = 50;
            $("textarea[id$=txtAddress]").keyup(function () {
                var len = $(this).val().length;
                if (len > limit) {
                    this.value = this.value.substring(0, limit);
                }
                $('#spn').text(limit - len + " characters left");
            });
        });
    </script>

 <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine"></asp:TextBox>
                    <span id="spn"></span>

No comments:

Post a Comment