如何验证GridView中的文本框以仅接受数字值 [英] How to validate textbox inside gridview to accept numeric values only

查看:99
本文介绍了如何验证GridView中的文本框以仅接受数字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在gridview项模板中验证文本框以仅接受数字值

How to validate textbox inside gridview item templates to accept numeric values only

推荐答案


试试这个:
HTML:
Hi,
Try this:
HTML:
<asp:TextBox ID="txtUID" runat="server" CssClass="TextBox" onkeypress="return onlyNumbers(this);"/>


JAVASCRIPT:


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



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



--Amit



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



--Amit


假定这不是一个Web应用程序,因为如果是这样,则您应该这样说,处理文本框的keypress或keydown事件,然后调用一个方法检查Char.IsDigit和Char.IsControl并将Handled = true设置为拒绝按键.如果这是一个Web应用程序问题,请正确标记您的问题.
Assuming this is not a web app, because if it was, you''d have said so, handle the keypress or keydown event for your textbox, and call a method that checks Char.IsDigit and Char.IsControl and set Handled = true so that the keypress is rejected. If this is a web app question, tag your questions properly.


看看RequiredFieldValidator 本文 [
Have a look at the RequiredFieldValidator[^].

This article[^] should also give you some updates.


这篇关于如何验证GridView中的文本框以仅接受数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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