且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何验证GridView中的文本框以仅接受数字值

更新时间:2023-10-12 14:14:28


试试这个:
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.