如何限制文本框仅插入数值而不是其他任何东西? [英] how do I restrict a text box from inserting only numeric values and not any thing else?
本文介绍了如何限制文本框仅插入数值而不是其他任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个文本框即。 txt_Long和txt_Lat。我希望只能将数值插入到这些文本框中,而不是数字或字母数字值。如果尝试插入除数值以外的任何其他值,则应在相应的文本框下面以红色显示验证违规消息。可以帮助吗?
I have 2 text boxes viz. txt_Long and txt_Lat. I want that only numeric values be able to be inserted into those text boxes and not numeric or alphanumeric values. If tried to insert any other values except numeric value, a validation violation message should be displayed in red color just under the respective text boxes.Can any one please help ?
推荐答案
如果您使用的是胜利表格
If you are using win forms
public void txtdigitonly_KeyPress(KeyPressEventArgs e)
{
try
{
if(char.IsDigit(e.KeyChar) == true)
{
e.Handled=false;
lblMessage.text="";
}
else
{
e.Handled=true;
lblMessage.text="Please Insert Numeric Value";
}
}
catch { }
}
lblMessage 是您要在其中显示消息的标签。
lblMessage is the label in which you want to display the message.
with with using AJAX工具: -
using with AJAX tool:-
<asp:TextBox ID="txtContactNo" runat="server" class="textfield" MaxLength="10"></asp:TextBox>
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" runat="server" ValidChars="0123456789"
TargetControlID="txtContactNo">
</cc1:FilteredTextBoxExtender>
或
与JavaScript一起使用: -
or
using with JavaScript:-
<script type="text/javascript" language="javascript">
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
if (charCode == 8)
return true;
return true;
}
</script>
<asp:TextBox ID="txtContactNo" onkeypress="return isNumberKey(event)" runat="server" class="textfield" MaxLength="10"></asp:TextBox>
试试这个js函数: -
try this js function:-
// To accept only Numeric Values
function IsNumberKeyOnly(event) {
var e = window.event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if ((charCode > 31) && (charCode < 48 || charCode > 57)) {
{
return false;
}
}
if (charCode == 8) { return true; }
return true;
}
这篇关于如何限制文本框仅插入数值而不是其他任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文