Maximum character length Validation (Maximum 8 characters allowed)
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{0,8}$" runat="server" ErrorMessage="Maximum 8 characters allowed."></asp:RegularExpressionValidator>
Minimum character length Validation (Minimum 8 characters required)
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox2" ID="RegularExpressionValidator2" ValidationExpression = "^[\s\S]{8,}$" runat="server" ErrorMessage="Minimum 8 characters required."></asp:RegularExpressionValidator>
Minimum and Maximum character length Validation (Minimum 5 and Maximum 8 characters required)
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox3" ID="RegularExpressionValidator3" ValidationExpression = "^[\s\S]{5,8}$" runat="server" ErrorMessage="Minimum 5 and Maximum 8 characters required."></asp:RegularExpressionValidator>
Have you tried setting the MaxLength Property on the TextBox? for Reference
This did it for me. I did not keep the MultiLine property.
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" MaxLength="2" Width="100%"></asp:TextBox>
Just type below two line in .cs page load
textbox1.Attributes.Remove("MaxLength");
textbox1.Attributes.Add("MaxLength", "30");
Hope it will help !
Add an extender of type FliterTextBoxExtender for your text box
it should appear like that
<asp:TextBox ID="TxtCellular" runat="server"></asp:TextBox>
<asp:FilteredTextBoxExtender ID="TxtCellular_FilteredTextBoxExtender"
runat="server" Enabled="True" TargetControlID="TxtCellular" FilterType="Numbers">
</asp:FilteredTextBoxExtender>
MaxLength
does not apply to ASP.NET to Textboxes with TextMode="MultiLine"
. An easy way to do this and keep your MultiLine
mark up would be to add:
onkeypress="return this.value.length<=10"
with 10 being the limit. Like so:
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" onkeypress="return this.value.length<=10" TextMode="MultiLine" Width="100%"></asp:TextBox>
EDIT Option One (Server side) The above example has some issues pointed out over the years. The underlying problem is a multi-line textBox
is rendered as a text-area and MaxLength
is removed. We can also fix in the C# or VB asp.net code behind of the page by forcing the re-attachment of MaxLength
instead of attaching onkeypress
.
protected void Page_Load(object sender, EventArgs e)
{
txtBodySMS.Attributes.Add("maxlength", "10");
}
EDIT Option Two (Client side): Here's a simple jquery solution. Include the following script in your head instead of attaching onkeypress
or editing the server side code.
$(document).ready(function () {
$(".MultiLineLimit").on('change keydown paste input', function () {
this.value = (this.value.length <= 10 ? this.value : this.value.substring(0, 10));
});
});
Now just add the MultiLineLimit
class to any of your <asp:TextBox>
textbox of TextMode="MultiLine"
.
<asp:TextBox ID="txtBodySMS" CssClass="MultiLineLimit" runat="server" Rows="10" TextMode="MultiLine" Width="100%"></asp:TextBox>
Remember in either scenario client side validation can be removed. If your storing this in a database it should always be validated server side as well.