Home > Back-end >  Textbox validation to make sure has integer values only
Textbox validation to make sure has integer values only

Time:11-18

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

CodePudding user response:

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

CodePudding user response:

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
  • Related