【问题标题】:how to use RegularExpressionValidator on textbox如何在文本框上使用 RegularExpressionValidator
【发布时间】:2011-01-20 12:33:26
【问题描述】:

我有一个需要验证的文本框,以便用户可以输入最多四个字符,并且它们可以是字母数字。我正在使用 VS2003,.NET 1.1。

请让我知道我应该使用什么表达式来验证这个条件 任何帮助都会很棒。谢谢!

试过这样:

<asp:TextBox id="wtxtTPP" tabIndex="2" runat="server" CssClass="text" Width="144px" MaxLength="4" />
<asp:RegularExpressionValidator id="RegularExpressionValidator1" style="z-index: 101; left: 208px; position: absolute; TOP: 16px" runat="server" ErrorMessage="RegularExpressionValidator" ValidationExpression="^([\S\s]{0,4})$" ControlToValidate="wtxtTPP" />
<input style="z-index: 102; left: 88px; position: absolute; top: 72px" type="submit" value="Submit" id="Submit1" name="Submit1" runat="server">

【问题讨论】:

标签: asp.net regex validation .net-1.1


【解决方案1】:

如您所说,使用正则表达式验证器并将表达式设置为如下所示:

^([\S\s]{0,4})$

将 4 替换为您想要的最大长度。

更新:

<asp:TextBox id="wtxtTPP" Runat="server" />

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" 
        ErrorMessage="RegularExpressionValidator" 
        ValidationExpression="^([\S\s]{0,4})$" 
        ControlToValidate="wtxtTPP" />

<asp:Button ID="Button1" runat="server" Text="Button" />

这对我来说很好用。我用普通的 asp.net 按钮替换了您的提交按钮,并简化了示例中所有不需要的内容。
一般来说,如果你只有一行文本框,你可以像你一样用MaxLength="4" 限制文本长度。不需要验证器。

【讨论】:

  • 嗨 Remy 我试过这样
  • 即使在文本框中输入“12as”也会显示错误信息
【解决方案2】:

就像@Remy 说的那样。

此外,正则表达式的 {0,4} 部分意味着长度应为零到最多四,因此将允许零长度,即 no 输入。如果数字是强制性的,请记住使用RequiredFieldValidator,或者用最少位数替换零。

【讨论】:

  • 实际上,RegularExpressionValidator 总是传递空白输入,就像除了RequiredFieldValidator 之外的所有验证器一样。
【解决方案3】:
       <table>
         <tr>   
           <td>E-mail Address:</td>
           <td>
              <asp:TextBox ID="txt_address" runat="server"></asp:TextBox>
           </td>
           <td>
             <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat ="server" 
              ErrorMessage="Please give Email Address! "
              ControlToValidate="txt_address"
              ValidationExpression="\S+@\S+\.\S+\w+"//example (example@yahoo.com)
              ForeColor="Red" >
             </asp:RegularExpressionValidator>
           </td>
         </tr>
       </table>

    <table>
      <tr>            
        <td>
           <asp:RadioButtonList ID="rbl_gender" runat="server">
            <asp:ListItem>Male</asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
            </asp:RadioButtonList>
         </td>
         <td>
            <asp:RequiredFieldValidator ID="RequiredFieldVlidator2" runat="server"
            ErrorMessage="Please Choose your Gender" ControlToValidate="rbl_gender"
            ForeColor="Red" >
            </asp:RequiredFieldValidator>
         </td>
       </tr>
   </table>

<table>
   <tr>    
     <td>
         <asp:RequiredFieldValidator ID="RequiredFeildValidator1" 
            runat ="server" ControlToValidate ="txt_name" 
            ErrorMessage="Please Insert Your name !" 
            ForeColor="Red">
         </asp:RequiredFieldValidator>
     </td>
   </tr>    

【讨论】:

    【解决方案4】:

    ValidationExpression 中的一个非常小的变化似乎会在仅有时工作和可靠工作之间产生差异。 以下内容在开发中对我有用,但在生产中无效:

    <asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "tbCarePlan" ID="revCarePlan" ValidationExpression = "^(.{0,4000})$" runat="server" ErrorMessage="Max 4000 characters allowed." />
    

    将上面的 ValidationExpression 替换为 "[\s\S]{0,4000}" 现在可以可靠地工作。省略 ^ 和 $ - RegularExpressionValidator 不需要这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多