locked
regular expression validator -VB.NET RRS feed

  • Question

  • User-1578974752 posted

    How can I use regular expression validator ,so that the text will allow only numbers not Decimals . If Decimals then show error message .Thanks

    Wednesday, January 30, 2019 9:09 AM

All replies

  • User-893317190 posted

    Hi shsu,

    You could try the code below.

       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
     
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="only allow number" ControlToValidate="TextBox1" ValidationExpression="\d+"></asp:RegularExpressionValidator>
            <asp:Button ID="Button1" runat="server" Text="Button" />

    If you want  the number start with 1-9 not 0 , you could write

     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="only allow number" ControlToValidate="TextBox1" ValidationExpression="[1,9]\d*"></asp:RegularExpressionValidator>

    Best regards,

    Ackerly Xu

    Thursday, January 31, 2019 1:59 AM
  • User-1578974752 posted

    This text box is inside the Grid view template field . Hence regular expression's control to validate field to that text box

    So is there any way that if I put . (decimal) in this textbox, then show error message

    Saturday, February 2, 2019 5:41 AM
  • User1512768972 posted

    If you can, try use HTML5 capabilities.. is more light..

    in your example

    <asp:TextBox ID="TextBox1" runat="server" type="number" min="1" max="100"  step="2"   ></asp:TextBox>

    and the browser its your job..

    Saturday, February 2, 2019 10:22 AM
  • User-893317190 posted

    Hi shsu,

    If you must use textbox of type text and not so familiar with jquery.

    You could try the code below.

     <asp:gridview runat="server" ID="gvw" AutoGenerateColumns="false">
              <Columns>
                  <asp:TemplateField HeaderText="number">
                      <ItemTemplate>
                          <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"  OnTextChanged="TextBox1_TextChanged" >
    
                          </asp:TextBox>
                          <asp:Label ID="Label1" runat="server" Text="you shouldn enter number without decimal"  Visible="false" ></asp:Label>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:gridview>

    Code behind.

     Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
            Dim box As TextBox = TryCast(sender, TextBox)
            'write a regex which only allow number without decimal
            Dim regex As Regex = New Regex("^\d*$")
            If Not regex.IsMatch(box.Text) Then
                ' if reges.IsMatch returns false
                ' show the error message
                box.NamingContainer.FindControl("Label1").Visible = True
    
            Else
                'else make the error message in visible
                box.NamingContainer.FindControl("Label1").Visible = False
            End If
    
    
    
        End Sub

    The result.

    Best regards,

    Ackerly Xu

    Monday, February 4, 2019 8:49 AM
  • User1156953606 posted

    thank you for information 

    Monday, February 4, 2019 12:32 PM