SweetTech
SweetTech

Reputation: 21

How to set Input Box max length propert

I want to add an input box with max-length property, But when I set its max length property to 500 and enter maximum text in input box, it's not taking maximum text. Its only working when I enter a limit text in Input Box.

Upvotes: 0

Views: 286

Answers (2)

Andy Brown
Andy Brown

Reputation: 5522

You could set a client side validator:

<body>
<form id="form1" runat="server">
<div>

    <!-- text box whose length should be limited -->
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <!-- validator to check this -->
    <asp:CustomValidator ID="CustomValidator1" runat="server" 
        ClientValidationFunction="CheckTextLength" ErrorMessage="Too long" />
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" />

</div>
</form>

You could then write a JavaScript function called CheckTextLength to check if the length of the textbox TextBox1 exceeds a certain length. Something like:

function CheckTextLength(source, arguments) {
        if (arguments.Value.length < 10) {
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
    }

If I knew JavaScript better, I'd give the exact answer! However, this approach relies on users having JavaScript enabled.

Upvotes: 0

Vikash Sinha
Vikash Sinha

Reputation: 193

In Multiline textbox you can not set max length. You have to check through javascript.

Upvotes: 2

Related Questions