Skip to main content
 首页 » 编程设计

c#之为什么我的正则表达式在 ASP.NET 中不起作用

2024年01月28日2698°冷暖

我想使用 RegularExpressionValidator 来表示 yyyy-mm-dd 形式的日期(例如:2012-11-29),这是我的表达方式:

/^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$/

我在 http://www.quanetic.com/Regex 上测试它它可以工作,但如果我在我的 asp.net 应用程序中这样做,它就不起作用

 <tr> 
   <td>Gültig ab:</td> 
   <td><asp:TextBox ID="txtVon" runat="server" ></asp:TextBox></td>   
   <td><asp:ImageButton ID="imgVon" runat="server" ImageUrl="images/Calender.ico" Width="15" Height="15" /> 
     <asp:CalendarExtender runat="server" ID="E_Von" TargetControlID="txtVon" Format="yyyy-MM-dd" PopupButtonID="imgVon"/></td>         
   <td> 
     <asp:RequiredFieldValidator ID="ValVon"  
       runat="server" ForeColor="red"  
       ErrorMessage="*" ControlToValidate="txtVon"></asp:RequiredFieldValidator> 
     <asp:RegularExpressionValidator ID="regVon"    
       runat="server" ControlToValidate="txtVon"  
       ErrorMessage="*Format" ForeColor="red"  
       ValidationExpression="/^(19[789]\d|20[0123]\d)\-(0\d|1[012]|\d)\-(31|30|[012]\d|\d)$/"></asp:RegularExpressionValidator> 
   </td> 
 </tr> 

错在哪里?

请您参考如下方法:

只需删除字符串开头和结尾的字符“/”即可。

你将拥有

ValidationExpression="^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$"