如何仅针对数字验证文本框。 [英] How to validate textbox only for number.
问题描述
我想在asp.net中只输入数字int文本框,如果输入字符则不允许输入值不会显示给我。
喜欢给定链接中的密码
https://eaadhaar.uidai.gov.in/
在我的代码中,我可以输入字符然后调用函数并对其进行验证然后删除。
我希望该用户在输入(字符)后无法看到字符。
我的给定代码在按键上单独控制文本框上工作。
我尝试过:
< asp:TextBox runat =serverID =txtPinCodeMaxLength =6Width =200onkeypress = return IsValidPinCode(event,this)>< / asp:TextBox>
function IsValidPinCode(e,t){
try {
var entered = S.特林跨度> .fromCharCode(e.which);
if (e.keyCode!= 8 ){
var regExpression = / ^ \d { 0 , 6 跨度>} $ /;
if (!regExpression.test(输入)){
e.preventDefault();
}
}
}
catch (错误){
alert(错误。描述);
}
}
从c#拨打电话无效
protected void grdSavedDDO_RowDataBound( object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState& DataControlRowState.Edit)> 0 )
{
TextBox txtPinCode =(TextBox)e.Row.FindControl( txtPinCode);
txtPinCode.Attributes.Add( onchange, javascript:return IsValidPinCode(' + e + ',' + 此 + '));
}
}
}
/;
if (!regExpression.test(输入)){
e.preventDefault();
}
}
}
catch (错误){
alert(错误。描述);
}
}
从c#拨打电话无效
protected void grdSavedDDO_RowDataBound( object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState& DataControlRowState.Edit)> 0 )
{
TextBox txtPinCode =(TextBox)e.Row.FindControl( txtPinCode);
txtPinCode.Attributes.Add( onchange, javascript:return IsValidPinCode(' + e + ',' + 此 + '));
}
}
}
如果我理解正确,你只想取消按键,如果不是数字。添加onkeypress
事件并将其绑定到此函数:
函数 pinCodeKeyPress(e){
var 输入= 字符串 .fromCharCode(e 。哪一个); // e.which是一个整数:char代码。 'entered'是单字符字符串
var regExpression = / ^ [0-9]
< BLOCKQUOTE> /;
if (!regExpression.test(输入)){
e.preventDefault(); // 'cancel'事件,字符不会出现在文本框中
}
}
重要:不仅要依赖客户端验证!用户可以轻松禁用此验证。不要忘记在服务器上添加一种验证。
I want enter only number int textbox in asp.net and if enter character it will not allowed and enter value does not show to me.
like pincode in given link
https://eaadhaar.uidai.gov.in/
In my code I am able to type character then function is called and validate it then remove.
I want to that user are not able to see character if entered(character).
My given code is working on text box in separate control on keypress.
What I have tried:
<asp:TextBox runat="server" ID="txtPinCode" MaxLength="6" Width="200" onkeypress="return IsValidPinCode(event,this)"></asp:TextBox>
function IsValidPinCode(e, t) {
try {
var entered = String.fromCharCode(e.which);
if (e.keyCode != 8) {
var regExpression = /^\d{0,6}$/;
if (!regExpression.test(entered)) {
e.preventDefault();
}
}
}
catch (err) {
alert(err.Description);
}
}
Calling from c# is not working
protected void grdSavedDDO_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
TextBox txtPinCode = (TextBox)e.Row.FindControl("txtPinCode");
txtPinCode.Attributes.Add("onchange", "javascript:return IsValidPinCode('" + e + "','" + this + "')");
}
}
}
/; if (!regExpression.test(entered)) { e.preventDefault(); } } } catch (err) { alert(err.Description); } }
Calling from c# is not working
protected void grdSavedDDO_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { TextBox txtPinCode = (TextBox)e.Row.FindControl("txtPinCode"); txtPinCode.Attributes.Add("onchange", "javascript:return IsValidPinCode('" + e + "','" + this + "')"); } } }
If I understand correctly, you just want to 'cancel' the key press if it's not a digit. Add aonkeypress
event and bind it to this function:
function pinCodeKeyPress(e) { var entered = String.fromCharCode(e.which); // e.which is an integer: the char code. 'entered' is a single-character string var regExpression = /^[0-9]
/; if (!regExpression.test(entered)) { e.preventDefault(); // 'cancel' event, the char won't appear in the textbox } }
Important: do not only rely on client-side validation! A user can easily disable this validation. Don't forget to add a sort of validation on your server, too.
这篇关于如何仅针对数字验证文本框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!