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

asp.net regex mixture of letters and numbers

我想在我的 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>

那我怎么才能得到呢?

提前致谢。

删除分隔符/:

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 class,其中 \d\D 默认适用于 Unicode 字符。