【问题标题】:Textbox validation to make sure has integer values only文本框验证以确保仅具有整数值
【发布时间】:2021-12-28 03:09:36
【问题描述】:

Example textbox 大家好,如何添加验证表达式以仅接受文本框中的数字和空格?如上图示例。我尝试这种方式 ValidationExpression="[0-100' ']{100,}" 但不正确..

<asp:TextBox ID="txtCopied" runat="server" TextMode="MultiLine" 
Height="200px" Width="617px" />
                                                       
<asp:RegularExpressionValidator ID="rvDigits" runat="server" 
ControlToValidate="txtCopied" ErrorMessage="Enter numbers only till 100 
digit" ValidationGroup="valGroup" ForeColor="Red" ValidationExpression=" 
[0-100' ']{100,}"  />

【问题讨论】:

    标签: asp.net vb.net validation


    【解决方案1】:
    <asp:RegularExpressionValidator ID="rvDigits" runat="server" 
    ControlToValidate="txtCopied" 
    ErrorMessage="Enter numbers only till 100 
    digit" ValidationGroup="valGroup" ForeColor="Red" 
    ValidationExpression="^[\d ]*$"  />
    

    【讨论】:

      【解决方案2】:

      我尝试使用 ServerValidate 而不是 RegularExpressionValidator 并试图弄乱它。

      <form id="form1" runat="server">
             <asp:TextBox ID="txtCopied" runat="server" TextMode="MultiLine" Height="200px" Width="617px" MaxLength="2000" />                                                                                                       
             <asp:Button ID="Button1" runat="server" Text="Button" />                                                     
             <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCopied" OnServerValidate="TextValidate"
               ValidateEmptyText="True" SetFocusOnError="True"  ErrorMessage="Enter numbers only till 100 digit" Display="Dynamic" />
       </form>
      
      Public Class WebForm1
         Inherits System.Web.UI.Page
      
         Protected Sub TextValidate(source As Object, args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
      
            Dim strRegex As String = "\b(0{0,2}[1-9]|0?[1-9][0-9]|1[1-9][0-9]|100)\b"
            Dim re As New Regex(strRegex, RegexOptions.Multiline)
            Dim ss As String() = args.Value.Split(" ")
      
            For i = 0 To ss.Length - 1
               args.IsValid = re.IsMatch(ss(i))
            Next
         End Sub
      End Class
      

      【讨论】:

        猜你喜欢
        • 2010-11-28
        • 2012-10-26
        • 1970-01-01
        • 2014-07-10
        • 1970-01-01
        • 2013-06-07
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多