Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to validate a textbox that it should allow only certain characters and after the limit is reached the textbox should not allow entering characters in it.It can be done by Javascript but dont know how to do:confused:Anyone please help..
Posted
Updated 28-Jul-10 18:41pm
v3

Go Through this code u will get it.....

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Without master page</title>
     <script type="text/javascript" src="js/JScript.js" ></script>
</head>
<body>
    <form id="form1" runat="server">
     <div> <br />
    <div id="lblMsg1">240 characters left</div>
    <asp:TextBox ID="TextBox1" runat="server" Height="50px"MaxLength="240"TextMode="MultiLine"Width="600px" ToolTip="Summary:(240 characters)"onkeyup="return validateLimit(this, 'lblMsg1', 240)"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="TextBox1" Display="Dynamic"
            SetFocusOnError="True">*</asp:RequiredFieldValidator>
    <br /><br /><br />
    <div id="lblMsg2">300 characters left</div>
    <asp:TextBox ID="TextBox2" runat="server" Height="50px" MaxLength="300" TextMode="MultiLine"Width="600px" ToolTip="Summary:(300 characters)"
                    onkeyup="return validateLimit(this, 'lblMsg2', 300)"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
            ControlToValidate="TextBox2" Display="Dynamic"
            SetFocusOnError="True">*</asp:RequiredFieldValidator>
    <br /> <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <br />
    </div>
    </form>
</body>
</html>
 
Share this answer
 
Comments
abi1988 29-Jul-10 1:00am    
But where is the JScript code?
koool.kabeer 29-Jul-10 4:53am    
where is Jscript.js file?
Here you go:
1. Tie a JavaScript function Validate() on textbox onKeyUp
2. in function Validate:

    a. Use Regular Expression to check for specific characters.
    b. Use textbox Value length as a constraint for maximum characters allowed.
    Return true/false accordingly.


Try now!
 
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