C# 检查输入是否为数字且仅包含数字 0-9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11009017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:05:07  来源:igfitidea点击:

Check if input is number and only contains digists 0-9

c#.net

提问by ShaneKm

I'm using C#. How would I verify whether an input string is an integer and is only made up of digits 0-9?

我正在使用 C#。如何验证输入字符串是否为整数并且仅由数字 0-9 组成?

An additional requirement is that it should be always made up of exacly 9 digits total; no more, no less.

一个额外的要求是它应该总是由总共 9 位数字组成;不多也不少。

e.g.,

例如,

OK: 118356737, 111111111, 123423141,

ERROR: 11a334356, 1.2.4.535, 1112234.222 etc

确定:118356737、111111111、123423141、

错误:11a334356、1.2.4.535、1112234.222 等

thanks

谢谢

采纳答案by Douglas

You can use either regex:

您可以使用任一正则表达式:

string input = "123456789";
bool isValid = Regex.IsMatch(input, @"^\d{9}$");

or LINQ:

或 LINQ:

string input = "123456789";
bool isValid = input.Length == 9 && input.All(char.IsDigit);

回答by gideon

Update. As per the comment, you need to use a regex to ensure all cases are handled correctly.

更新。根据评论,您需要使用正则表达式来确保正确处理所有情况。

Check for correct expression with regex

使用正则表达式检查正确的表达式

string inputStr = "";
if(Regex.IsMatch(inputStr, @"^\d{9}$");) 
{
 //now check for int
 int result;
 if(int.TryParse(inputStr, out result) 
 {
   //it IS an integer 
   //the result integer is in the variable result.
 }
}

See msdn for more on int.TryParse(). Note: double, float, long etc also have their versions of TryParse().

有关int.TryParse() 的更多信息,请参阅 msdn 。注意:double、float、long 等也有它们的TryParse().

回答by Zoya

add this ajax control

添加这个ajax控件

<

<

asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender5" runat="server" TargetControlID="yourtextbox"
                                    FilterType="Custom, Numbers" ValidChars="." />

or use regular expression in validation toolbox

或在验证工具箱中使用正则表达式

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" Style="z-index: 101; left: 424px; position: absolute;
top: 285px" ValidationExpression="^\d+$" ValidationGroup="check"></asp:RegularExpressionValidator>

回答by Green Su

You can use regular expression to verify the input string. The pattern below matches 9 numbers, and the first number should not be 0.

您可以使用正则表达式来验证输入字符串。下面的模式匹配 9 个数字,第一个数字不应为 0。

^[1-9]\d{8}$

^[1-9]\d{8}$

回答by Gowtham

if you want to work for decimal point, try this

如果你想为小数点工作,试试这个

string input="12356";
bool valid= Regex.IsMatch(input, @"^[-+]?\d+\.?\d*$")); // returns true;



string input="123.456";
bool isValid= Regex.IsMatch(input, @"^[-+]?\d+\.?\d*$")); //returns true


string input="12%3.456";
bool isValid= Regex.IsMatch(input, @"^[-+]?\d+\.?\d*$")); //returns false

Click here For Detailed Description

单击此处查看详细说明