Bayan
Bayan

Reputation: 82

validation tools in asp.net

i have a registration form in asp.net i want to make a failed to user comments it is a multiline textbox and i want to let the use write only 100 character in this box and if he enter more.. i want to only accept the first 100 character and delete the other then send the user error massage with the accepted part of text i have tried to use RegularExpressionValidator like this:

<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="TextBox1" ErrorMessage="Comment should be less the 100 letter." ForeColor="Red" ValidationExpression="^.{1,100}$"></asp:RegularExpressionValidator>

but i still want yo show the user the accepted part and to delete the rest from the box so how could i do this using validation tools

Upvotes: 0

Views: 63

Answers (1)

Hardik Mer
Hardik Mer

Reputation: 836

just add two lines on your Page_Load event

TextBox1.Attributes.Remove("MaxLength");
TextBox1.Attributes.Add("MaxLength", "100");

this will make it work even if you set TextBox to MultiLine mode.

With javascript you can do like this

<script type="text/javascript">
        function LimtCharacters(txtMsg, CharLength, indicator) {
            chars = txtMsg.value.length;
            document.getElementById(indicator).innerHTML = CharLength - chars;
            if (chars > CharLength) {
                txtMsg.value = txtMsg.value.substring(0, CharLength);
            }
        }
</script>

write this on your aspx page

Number of Characters Left:

<label id="lblcount" style="background-color:#E2EEF1;color:Red;font-weight:bold;">100</label><br/>

<asp:TextBox ID="multiTxtBox"  TextMode="MultiLine" onkeyup="LimtCharacters(this,100,'lblcount');" runat="server"></asp:TextBox>

Hope it will help!

Upvotes: 1

Related Questions