Mvc 验证正则表达式只有数字? [英] Mvc validation regular expression only numbers?
本文介绍了Mvc 验证正则表达式只有数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Mvc 网络应用程序中尝试了以下代码,用于仅数字验证的联系人号码验证.
I tried the following code for digits-only validation for a contact number validation in Mvc web app.
[RegularExpression(@"/(^(d{10})?)$/", ErrorMessage = "Please enter proper contact details.")]
[Required]
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
但是验证表达式不起作用.
But the validation expression is not working.
对于联系电话,我只想接受数字.它可以是 10 位手机号码或固定电话号码.
For the contact number I want to only accept digits. It can be either a 10 digit mobile number or a land-line number.
推荐答案
//
是一种构建正则表达式字面量对象的 javascript 方式.在 .NET 中你不应该使用它.
/ /
is javascript way to build a regular expression literal object. In .NET you should not use it.
尝试以下操作:
@"^((d{10}?))$"
或者如果您想要正好 10 位数字:
or if you want exactly 10 digits:
@"^(d{10})$"
这篇关于Mvc 验证正则表达式只有数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文