限制使用Javascript在文本框中输入的字符 [英] restricting characters entered in textbox using Javascript

查看:86
本文介绍了限制使用Javascript在文本框中输入的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证一个文本框,该文本框应仅允许某些字符,并且在达到限制后,该文本框不应允许在其中输入字符.可以通过Javascript完成,但不知道该怎么做:confused:任何人都请帮忙..

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..

推荐答案

通过这段代码,您将得到它...

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

<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>


您在这里:
1.在文本框onKeyUp上绑定一个JavaScript函数Validate()
2.在功能中验证:
Here you go:
1. Tie a JavaScript function Validate() on textbox onKeyUp
2. in function Validate:

    一个.使用正则表达式检查特定字符.
    b.使用文本框值长度"作为允许的最大字符数的约束.
    相应地返回true/false.

    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!


这篇关于限制使用Javascript在文本框中输入的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆