如何修复TextBox中的字符长度 [英] How to fix Character Length in a TextBox

查看:88
本文介绍了如何修复TextBox中的字符长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

如何在文本框中修复字符长度?假设我想限制为3个三个字符。它不仅应输入少于或多于三个字符。其次,我想只允许文本框中的文本。请帮帮我。

解决方案

1.要限制三个字符的文本框的最大长度,写一下

< asp:TextBox ID =TextBox1runat =serverMaxLength =3>< / asp:TextBox> 





2.在文本框中正好匹配三个字符添加以下javascript函数



  function  checkLength(obj){
if (obj.value.length!= 3 ) {
alert( 应该是三个char。);
obj.focus();
}
}





并在onblur活动中调用该函数

 TextBox1.Attributes.Add(onblur,checkLength(this)); 





3.仅允许文本框中的文本添加以下javascript函数

  function  handleKeydown(event){
keyEntry = event .keyCode || event.which;
if
((keyEntry> = ' 65')&&(keyEntry< = ' 90'))||
((keyEntry> = ' 97')& &(keyEntry< = ' 122'))||
(keyEntry == ' 46')||
(keyEntry == ' 32')||
keyEntry == ' 45' ||
keyEntry == ' 46' ||
keyEntry == ' 8' ||
keyEntry == ' 35' ||
keyEntry == ' 36' ||
keyEntry == ' 9'
返回 true ;
else
return false < /跨度>;
}





并添加onkeydown&代码背后的onpaste处理程序

 TextBox1.Attributes.Add(onkeydown,return handleKeydown(event)); 
TextBox1.Attributes.Add(onpaste,return handleKeydown(event));







希望这会对你有帮助。


touseef4pk写道:

说我想限制为3个三个字符。它不仅应该输入少于或多于三个字符。





有很多方法:

1)使用Javascript - 编写文本框的代码onblur来检查长度是否等于3。

 <   asp:TextBox     ID   =  TextBox1    runat   =  server    onblur   =  javascript:validate( ); >  <   / asp:TextBox  >  



  function  validate()
{
if document .getElementById( TextBox1)。value.length!= 3
{
alert( text lenghth必须为3);
document .getElementById( TextBox1< /跨度>)选择();
document .getElementById( TextBox1< /跨度>)聚焦();
return false ;
}
其他
{
返回 ;
}
}





2)使用 RegularExpressionValidator 。你需要写一个正则表达式来检查输入的值是3个字符。





touseef4pk写道:

请告诉我如何编写reqular表达式来限制文本框中最多三个字符的字符



试试这个:\ b [a-zA-Z] {3} \b



[/编辑]




touseef4pk写道:

我想只允许文本框中的文字。





文本是指字母数字,这是文本框的默认值。

我猜你不是那个意思。请澄清。


感谢您的回复。请告诉我如何编写reqular表达式来限制文本框中最多三个字符的字符。 Actualy我的意思是说输入shoule只是字母,只有The。它不应该接受数值。请帮我解决这个冲突。


Dear fellow,
How can I fix character length in textbox? Say i want to restrict to 3 three characters. It should not only to enter less than or more than three characters. And secondly, I want to allow only text in the textbox. Please help me out.

解决方案

1.To restrict maxlength of textbox with three character, write

<asp:TextBox ID="TextBox1" runat="server" MaxLength="3"></asp:TextBox>



2.To match exactly three character in textbox add following javascript function

function checkLength(obj) {
            if (obj.value.length != 3) {
                alert("Should be exactly three char.");
                obj.focus();
            }
        }



And call the function in onblur event

TextBox1.Attributes.Add("onblur", "checkLength(this)");



3.To allow only text in textbox add following javascript function

function handleKeydown(event) {
            keyEntry = event.keyCode || event.which;
            if (
                ((keyEntry >= '65') && (keyEntry <= '90')) ||
                ((keyEntry >= '97') && (keyEntry <= '122')) ||
                (keyEntry == '46') ||
                (keyEntry == '32') ||
                keyEntry == '45' ||
                keyEntry == '46' ||
                keyEntry == '8' ||
                keyEntry == '35' ||
                keyEntry == '36'||
                keyEntry == '9')
                return true;
            else
                return false;
        }



And add onkeydown & onpaste handler in code behind

TextBox1.Attributes.Add("onkeydown", "return handleKeydown(event)");
TextBox1.Attributes.Add("onpaste", "return handleKeydown(event)");




Hope this will help you.


touseef4pk wrote:

Say i want to restrict to 3 three characters. It should not only to enter less than or more than three characters.



There are many ways:
1) Use Javascript - Write code onblur of the textbox to check if the length is equal to 3 or not.

<asp:TextBox ID="TextBox1" runat="server" onblur="javascript:validate();"></asp:TextBox>


function validate()
    {
        if(document.getElementById("TextBox1").value.length != 3)
        {
            alert("text lenghth must be three");
            document.getElementById("TextBox1").select();
            document.getElementById("TextBox1").focus();
            return false;
        }
        else
        {
            return true;
        }
    }



2) Use RegularExpressionValidator. You need to write a regular expression which checks the value entered is 3 characters.

[Edit]

touseef4pk wrote:

Please tell me how can i write reqular expression to limit characters upto three characters in textbox


Try this: \b[a-zA-Z]{3}\b

[/Edit]


touseef4pk wrote:

I want to allow only text in the textbox.



Text means alphanumeric which is the default for a textbox.
I guess you don't mean that. Please clarify.


Thanks for reply. Please tell me how can i write reqular expression to limit characters upto three characters in textbox. Actualy I mean to say that the input shoule be alphabetical only like "The". It should not accept numeric values. Please help me to resolve this conflict.


这篇关于如何修复TextBox中的字符长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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