文本框中仅允许输入字符,不允许输入数字 [英] Allow only character in text box, not number

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

问题描述

我们只想在文本框中插入字符,而不是数字.

这怎么可能 ?请告诉我.

解决方案

使用该RegularExpressionValidator

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox1" ValidationExpression="[aA-zZ]"></asp:RegularExpressionValidator> 


根据您的标签,它是一个网络应用程序,因此您需要编写一些JavaScript验证代码. "onchange"是DOM中与TextChanged对应的事件.您还应该处理"onkeypress"并拒绝数字,因为在onchange上修改文本会导致插入符号的位置被重置,并且如果每次输入无效字符都会发生这种情况,这将很烦人.


您也可以使用简单的 javascript :
执行此操作

函数AllowAlphabet(e)
{
  isIE = document.all吗?  1 : 0 
  keyEntry =!isIE吗? e.哪个:事件 .keyCode;
  如果(((((keyEntry >  = '  65')&&(keyEntry <  = '  90'))||((keyEntry >  = '  97')&&(keyEntry <  = '  122'))||(keyEntry ==  '  46')||(keyEntry == ' ' 返回 其他
{
    alert(' 请仅输入字符值.');
    返回 ;
      }
}

< asp:textbox id = "  runat =  "  maxlength = "  onkeypress = "  xmlns:asp =  #unknown" </ >  


We want to insert only character in text box not number.

How is this possible ? Please tell me.

解决方案

use that RegularExpressionValidator

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox1" ValidationExpression="[aA-zZ]"></asp:RegularExpressionValidator> 


It''s a web app, according to your tags, so you need to write some JavaScript validation code. ''onchange'' is the event in the DOM that corresponds to TextChanged. You should also handle ''onkeypress'' and reject numbers, because modifying the text in onchange causes the caret position to be reset and that''s annoying if it happens every time you enter an invalid character.


You can also do this using simple javascript:

function AllowAlphabet(e)
{
  isIE = document.all ? 1 : 0
  keyEntry = !isIE ? e.which : event.keyCode;
  if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
     return true;
  else
{
    alert('Please Enter Only Character values.');
    return false;
      }
}

<asp:textbox id="TextBox1" runat="server" maxlength="1" onkeypress="return AllowAlphabet(event)" xmlns:asp="#unknown"></asp:textbox>


这篇关于文本框中仅允许输入字符,不允许输入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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