Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am Using MultiLine Textbox now i want to allow all the characters in it and also i want to set the limit of the textbox to 150 characters. So iwant to use the regular expression so any one can help me.
i am using this regular expression:

"^[A-Za-z0-9\s!@#$%^&*()_+=-`~\\\]\[{}|';:/.,?><]*{1,150}$"

All working fine but it does not limit the characters to 150

Thanks in advance.
Posted
Comments
murkalkiran 28-Oct-14 2:22am    
onkeypress="return this.value.length<=10" //replace 10 with the max length you want
Robert Welliever 28-Oct-14 2:41am    
That is about as beautiful as code gets. More of a solution than a comment though.
Member 11099119 28-Oct-14 2:42am    
No i Want it in regular expression only
Anyway thank you
Robert Welliever 28-Oct-14 3:04am    
That doesn't make any sense. A regular expression wouldn't set a limit on the number of characters entered but murkialkiran's code would. Are you sure you know what question you asked? You have to latch onto one of the textbox's events in order to know if someone is altering the text.
Member 11099119 28-Oct-14 3:08am    
oh ok thank you

1 solution

XML
<script language=JavaScript>
<!-- Beginning of JavaScript -
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
    } else {
        countfield.value = maxlimit - field.value.length;
    }
}
//  End -->>
</script>
<html>
    <head>
        <meta name="GENERATOR" Content="ASP Express 4.5">
        <title>Untitled</title>
    </head>
    <body>
        <form id="form1" Runat="server">
            Characters Left:
                <asp:TextBox
                    BackColor="Blue"
                    BorderStyle="None"
                    Width="30"
                    ForeColor="White"
                    Font-Bold="True"
                    id="txtLen"
                    Runat="server" Text="100" /><br>
            Enter comments:<br>
            <asp:TextBox id="txtmessage"
                Columns="40" Rows="4"
                TextMode="MultiLine"
                onkeydown="textCounter(this.form.txtmessage,this.form.txtLen,100)"
                onkeyup="textCounter(this.form.txtmessage,this.form.txtLen,100)"
                Runat="server" />
        </form>
    </body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900