正则表达式仅限制字符串字段中的数字

时间:2014-09-22 11:11:31

标签: regex asp.net-mvc

是否可以仅使用正则表达式来限制字符串字符串字段中的数字?

例如:123456(不允许),12Test(Allwed),测试(允许),测试#@ 12(允许)

这可能吗?

4 个答案:

答案 0 :(得分:0)

您可以尝试以下代码来匹配数字。 System.Text.RegularExpressions.Regex.IsMatch(value,“^ [0-9] * $”);

您也可以尝试使用^ \ d + $或regex = new Regex(“^ [0-9] + $”);正则表达式。

答案 1 :(得分:0)

也许这种非消费正则表达式可以提供帮助

[0-9]*(?=([a-zA-Z\W|_]+))

答案 2 :(得分:0)

ViewSource

中显示以下内容
data-val-regex-pattern="([a-zA-Z0-9 .&'-]+)"

模型示例: -

[StringLength(100)]
[Display(Description = "Name")]
[RegularExpression("([a-zA-Z0-9 .&'-]+)", ErrorMessage = "Enter only alphabets and numbers of Name")]
public string Name { get; set; }

ViewSource:

<input data-val="true" data-val-length="The field Name must be a string with a maximum length of 100." data-val-length-max="100" data-val-regex="Enter only alphabets and numbers of Name" data-val-regex-pattern="([a-zA-Z0-9 .&amp;amp;&amp;#39;-]+)" id="Name " maxlength="100" name="Name " type="text" value="" />

答案 3 :(得分:0)

这种模式应该只限制数字

^(?=.*[^0-9\r\n]).*$  

预测多行字段中的非数字或

^(?=.*[^0-9]).*$  

在单行字段中