限制Asp:TextBox不允许超过6个字符. [英] Restrict Asp:TextBox to not allow more than 6 character in it.

查看:211
本文介绍了限制Asp:TextBox不允许超过6个字符.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想限制用户不能在文本框中输入超过6个字符.当用户开始在文本框中输入并达到最大限制并尝试输入第7个字符时,文本框将不允许他这样做并警告他已达到最大尺寸.我该如何实现?

我已经在代码中添加了JavaScript函数,但是没有在我调用该函数的事件上得到该函数,当前我正在on-blur上进行调用,但是在文本框中输入的字符不能超过9个,之后单击IM

Hi,

I want to restrict the user that he cant enter more than 6 characters in a text box.As a user start entering in the text box and reach to the max limit and try entering 7th character, the text box will not allow him to do so and alert him that max size reached.How i achieve this?

I had added JavaScript function in my code to do so but not getting that on which event i call this function , currently i am calling on on-blur but i m able to type more than 9 character in the text box and after that when i m clicking on a button then i m getting alert message.i want that while typing in the text box itself if max limit reached the user get prompted.

推荐答案

尝试使用MaxLength 属性 [ ^ ].


使用文本框"属性: ^ ]
Use Textbox property: MSDN: Textbox.MaxLength[^]


基于后续问题:我希望在达到最大限制的情况下在文本框中输入内容,提示用户".这不会提示.
步骤:
1.为文本框定义一个onkeyup
2.将Javascript函数绑定到onKeyup
3.检查长度和提示.

样本:
HTML:
Based on followup question: "i want that while typing in the text box itself if max limit reached the user get prompted". This will not prompt.
Steps:
1. Define a onkeyup for the textbox
2. Tie up a Javascript funtion to onKeyup
3. check the length and prompt.

Sample:
HTML:
<input type"text" id="mytb" onkeyup="CheckLength(this.value);"/>


JS代码:


JS Code:

function CheckLength(currentValue)
{
   if(currentValue.length > 6)
      alert("Only 6 letters are allowed, please follow the rule!");
}



您可以根据需要修改实现.



You can modify the implementation as per your need.


这篇关于限制Asp:TextBox不允许超过6个字符.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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