如何检查texbox是否只有数字。 [英] How to check if a texbox has only numbers.

查看:112
本文介绍了如何检查texbox是否只有数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框'Mobile'。我正在使用C#代码来检查输入的值是否为数字。但它只检查最多9位数。我希望检查10位数字,即手机号码。



aspx



< asp:TextBox ID =Mobilerunat =serverBorderColor =Blackclass =textboxStyle =text-transform:uppercaseWidth =80pxMaxLength =10>

< br $>


C#



int parsedValue;

if(!int.TryParse( Mobile.Text,out parsedValue))

{

标签lbl1 =新标签();

lbl1.Text =请仅输入数字在手机号码;

ScriptManager.RegisterStartupScript(this,this.GetType(),ShowSuccess,javascript:Showalert('+ lbl1.Text +'),true);

返回;

}

解决方案

改为使用正则表达式:

< pre lang =cs> public static Regex isMobileNo = 正则表达式( @ ^ \d {1 0}

);
...
bool IsMatch = isMobileNo.IsMatch(Mobile.Text);


简单,只需将MaxLength =10更改为MaxLength =11:)


I have a textbox 'Mobile'. I'm using a C# code to check if the entered value is a number or not. But it only checks upto 9 digits. And I want 10 digits to be checked i.e. Mobile Number.

aspx

<asp:TextBox ID="Mobile" runat="server" BorderColor="Black" class="textbox" Style="text-transform:uppercase" Width="80px" MaxLength="10">


C#

int parsedValue;
if (!int.TryParse(Mobile.Text, out parsedValue))
{
Label lbl1 = new Label();
lbl1.Text = "Please Enter only Numbers in Mobile No.";
ScriptManager.RegisterStartupScript(this,this.GetType(),"ShowSuccess","javascript:Showalert('" + lbl1.Text + "')",true);
return;
}

解决方案

Use a regex instead:

public static Regex isMobileNo= new Regex(@"^\d{10}


"); ... bool IsMatch = isMobileNo.IsMatch(Mobile.Text);


Easy, just Change MaxLength="10" to MaxLength="11" :)


这篇关于如何检查texbox是否只有数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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