将文本框内的值限制为仅限数字,但该文本框位于网格视图中 [英] Restricting the values inside textbox to only numbers but that text box is within grid view

查看:71
本文介绍了将文本框内的值限制为仅限数字,但该文本框位于网格视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨....我正在使用网格视图并在网格视图内部显示一个文本框...但该文本框应限制用户输入特殊字符或字母..它应该只接受并且只有数字介于两者之间1到100它也可以像2.5或20.5 ...那么验证文本框的最佳方法是什么...在网格视图中最好使用像正则表达式这样的验证器,还是有其他方法... 。在网格视图中会有很多行..所以最好的方法是验证网格视图中的文本框请帮助或指导我...





提前感谢你。

解决方案

请按照如何验证gridview中的文本框以仅接受数值 [ ^ ]。



HTML:

 <   asp:TextBox     ID   =  txtUID    runat   =  server    CssClass   =  TextBox    onkeypress   = 仅返回数字(此);  /  >  



JAVASCRIPT:

//将用户限制为键入命令chrectors和其他sp ecial charectors

  function  onlyNumbers(evt){
var e = event || EVT; // 用于跨浏览器兼容性
var charCode = e.which || e.keyCode;
if (charCode> 31 &&(charCode< 48 || charCode> 57 ))
return false ;
return true ;
}



如果用户需要键入字段,也可以使用RequiredFieldValidator。



您可以添加逻辑来限制函数 onlyNumbers(evt)中的数字范围。



谢谢,

Tadit。


在你的网格视图中声明文本框这样



< pre lang =c#>< itemtemplate>
< asp:textbox id = txtUID runat = server cssclass = TextBox onkeypress = return isNumberKey(event); xmlns:asp = #unknown />
< / itemtemplate >





并在页面的head部分添加脚本

 < script     type   =  text / javascript >  

函数isNumberKey(evt){

var charCode =(evt.which)? evt.which: event .keyCode
if (charCode > 31 &&(charCode < 48 || charCode > 57 ))
返回 false ;
return true ;
}

< / script >









希望这有帮助你



享受:)


hi.... i am using a grid view and inside the grid view a text box... but that text box should restrict user from entering special characters or alphabets.. it should accept only and only numbers ranging between say 1 to 100 it can also be like 2.5 or 20.5 ... so what is the best way to validate the text box... inside the grid view is it better to use a validator like regular expression or is there any other way... as in grid view there will be many rows.. so whats the best way to validate the text box within grid view please help or guide me...


Thanking you in advance.

解决方案

Please follow the solution in How to validate textbox inside gridview to accept numeric values only[^].


HTML:

<asp:TextBox ID="txtUID" runat="server" CssClass="TextBox" onkeypress="return onlyNumbers(this);"/>


JAVASCRIPT:
//Restrict the user to key-in chrectors and other special charectors

function onlyNumbers(evt) {
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}


And also use RequiredFieldValidator if the field is required to key-in by user.


You can add your logic for restricting the number ranges in function onlyNumbers(evt).

Thanks,
Tadit.


Declare textbox within ur grid view like this

<itemtemplate>
<asp:textbox id="txtUID" runat="server" cssclass="TextBox" onkeypress="return isNumberKey(event);" xmlns:asp="#unknown" />
</itemtemplate>



and in head section of the page add script

<script type="text/javascript">

function isNumberKey(evt) {
    
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
    return true;
}

</script>





hope this helps u

enjoy :)


这篇关于将文本框内的值限制为仅限数字,但该文本框位于网格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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