asp.net正则表达式字母和数字的混合

时间:2015-01-15 07:51:56

标签: asp.net regex

我想在ASP.Net项目中混合使用字母和数字来表示产品代码。它应该有4个大写字母和3个数字的混合,例如, A1B2C3D,ABCD123,A123BCD

我做了这个,但它不起作用

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
         ControlToValidate="ProductCode" ErrorMessage="Must be 7 characters in length and a mixture of 4 uppercase characters and 3 numerals " ForeColor="Red" 
         ValidationExpression="/^(?=(?:[^A-Z]*[A-Z]){4}[^A-Z]*$)(?=(?:\D*\d){3}\D*$)[A-Z\d]{7}$/"></asp:RegularExpressionValidator>

那我怎么能得到它?

提前致谢。

1 个答案:

答案 0 :(得分:0)

删除分隔符/

ValidationExpression="^(?=(?:[^A-Z]*[A-Z]){4}[^A-Z]*$)(?=(?:\D*\d){3}\D*$)[A-Z\d]{7}$">

RegularExpressionValidator.ValidationExpression Property文档中的示例没有/,因此将它们包含在正则表达式中是错误的。

您可能希望将\d\D更改为[0-9][^0-9]。如果内容在服务器端验证,它可能会通过Regex类,其中\d\D默认使用Unicode字符。

相关问题