0

I need help to restrict my textbox to accept just numbers. I have another textbox event textchanged and in that condition I want to not allow user to write other characters than numbers. have implemented the maxlength but I can't validate to accpet just numbers. here is my condition that I want to implement in:

 Protected Sub txtIdType_TextChanged(sender As Object, e As EventArgs)
      Dim txtb As TextBox = CType(FormViewPerson.FindControl("txtIdType"), TextBox)
      Dim txtb1 As TextBox = CType(FormViewPerson.FindControl("TextBoxIDCode"), TextBox)

      If (txtb.Text = "Leternjoftimi" OrElse txtb.Text = "KosovoIDCard" OrElse txtb.Text = "Licna karta") Then
           txtb1.MaxLength = 10
      End If
 End Sub

3 Answers 3

2

You can do this simply in aspx page, like below:-

<asp:TextBox ID="txtNumbers" runat="server" autocomplete="off" ValidationGroup="AddNew"></asp:TextBox>
<asp:RegularExpressionValidator ID="regNumbers" runat="server" ErrorMessage="Only numbers are allowed" ControlToValidate="txtNumbers" ValidationExpression="^[0-9]*$" ValidationGroup="AddNew"></asp:RegularExpressionValidator>

You don't need to use the code-behind for this. Hope this helps

Sign up to request clarification or add additional context in comments.

4 Comments

I need just when txtb.Text = "Leternjoftimi" txtb1 to write just numbers not for all types
Will always txtb.Text = Leternjoftimi and where you will write only numbers. Please specify your doubts clearly. As by your question you only need the textbox to allow only numbers.
Yeah just for this case (txtb.Text = "Leternjoftimi") I want the other textbox to be the maxlength = 10 and not to allow to write othercharacters than numbers
@user2568960: I guess, you should try enb081's solution
1

You can add the validator from code behind. Try to do the following:

 Protected Sub txtIdType_TextChanged(sender As Object, e As EventArgs)
            Dim txtb As TextBox = CType(FormViewPerson.FindControl("txtIdType"), TextBox)
            Dim txtb1 As TextBox = CType(FormViewPerson.FindControl("TextBoxIDCode"), TextBox)

            If (txtb.Text = "Leternjoftimi" OrElse txtb.Text = "KosovoIDCard" OrElse txtb.Text = "Licna karta") Then

            txtb1.MaxLength = 10
            Dim validator As New RegularExpressionValidator()
            validator.ID = "validator" + New Random().[Next](100, 1000)
            validator.ControlToValidate = CType(FormViewPerson.FindControl("TextBoxIDCode"), TextBox).ID
            validator.ValidationExpression="^[0-9]*$"    
            FormViewPerson.Controls.Add(validator)

            End If

        End Sub

Suksese!

6 Comments

can you check in the browser if there is rendered a span with some ID that contains the validator word?
See my edit: try changing Me.Controls.Add(validator) to FormViewPerson.Controls.Add(validator)
Thank you, but still it's not working, it allows me to write other characters not just numbers :S
@user2568960 does it allow you to write more than 10 characters?
Yes when I add this piece of code from enb081 then it allos me to write more than 10. If I let the code how it was originally it doesnt allow me to write more than 10 characters
|
1

I solved my problem using this function :

 For Each ch As Char In txt.Text
                If Not Char.IsDigit(ch) Then
                    txt.Text = ""
                    Exit Sub
                End If
            Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.