Tuesday, 16 October 2012

Text Area Validation for maximum length in java script and asp.net


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextAreaValidation.aspx.cs"Inherits="TextAreaValidation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head runat="server">
    <title></title>
    <script type="text/javascript">

        function TextAreaValidation(field, count, max)
        {
            if (field.value.length > max)
             {
                field.value = field.value.substring(0, max);
                field.blur();
                field.focus();
                return false;
            }
            else
            count.value = max - field.value.length;
           
        }
   </script>
  
</head>
<body>
    <form id="form1" runat="server">

<asp:TextBox runat="server" onblur="TextAreaValidation(this,this.form.counter,500);"
        onkeyup="TextAreaValidation(this,this.form.counter,500);"
        style="width: 352px; height: 74px" rows="5" cols="10" id="txtArea" TextMode="MultiLine" ></asp:TextBox>
<br />
Maximum Character<input type="text" onblur="TextAreaValidation(this.form.recipients,this,500);" disabled="disabled" onfocus="this.blur();"   value="500" name="counter" />
</form>
</body>
</html>

No comments:

Post a Comment