skip to Main Content

Example textbox
Hi All, how can i add validate expression to only accept numeric and space in my textbox? Example as image above. I try this way ValidationExpression="[0-100′ ‘]{100,}" but not correct..

<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,}"  />

2

Answers


  1. <asp:RegularExpressionValidator ID="rvDigits" runat="server" 
    ControlToValidate="txtCopied" 
    ErrorMessage="Enter numbers only till 100 
    digit" ValidationGroup="valGroup" ForeColor="Red" 
    ValidationExpression="^[d ]*$"  />
    
    Login or Signup to reply.
  2. I tried using ServerValidate instead of RegularExpressionValidator and tried to mess around with it.

    <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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search