I am currently working on a project that has a textbox I need to do a validation on. I want to actually do two validations: 1) Make sure it is an integer and 2) Make sure it is divisible by 10.

I have done a lot of searching and have read about the "mod" method but am not familiar with that at all.

Does anyone here have any ideas??


The first thing I'd suggest is using a MaskedTextBox. You can set the format to be only integers. Then handle the Validated event to check for divisibility by ten. The mod operator returns the remainder from a division. So if the quotient is 0 the dividend is exactly divisible by the divisor

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MaskedTextBox1.ValidatingType = GetType(Integer)
    MaskedTextBox1.Mask = "0000"
End Sub

Private Sub MaskedTextBox1_Validated(sender As Object, e As EventArgs) Handles MaskedTextBox1.Validated
    If Integer.Parse(MaskedTextBox1.Text) Mod 10 <> 0 Then
        Windows.Forms.MessageBox.Show("Enter a multiple of 10 please.")
    End If
End Sub
commented: You're just smokin' these days. Don't you have a day job? +12

Just a note on using Mod, 0 Mod (any number) = 0 so you should also check your input is greater than 0 (unless of course, zero is an acceptable input)

Thanks for your help! I appreciate that. I have a greater understanding for the MOD now. I am now trying to figure out how to even verify if it is a number that was input and not a letter.


I have tried mulitple ways to incorporate the Mod with my validating whether a person actually enters an integer or not and I keep gettng a different error every time. I am just using the standard if/then statement.

If you post your actual code it would make it a lot easier to understand what your problem might be.


Try the IsNumeric to check if your input is numeric or not:

If Isnumeric(MyInput) then
    'Do something
    Msgbox("Non Numeric input detected")
end if

Stop annoying users with unwanted Msgboxes saying: "He dummy! You typed a letter!"
A Masked textbox might be ok, but I would rather use it for social security codes etc.
BTW, you still have to validate for divisibility by 10.
Try handling a KeyDown/KeyPress event.
Here you can only allow digits and even test if the last digit entered was zero.

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    If IsNumeric(TextBox1.Text) Then
        If Integer.Parse(TextBox1.Text) Mod 100 <> 0 Then
            Windows.Forms.MessageBox.Show("Enter a multiple of 100 please")
        End If
    End If
End Sub

This is the code I have been basically working with so far. There are four textboxes that all require integers for the program to work. Plus they have to be divisible by Either 100, 50, or 20. This code I posted allows me to check for the integer being divisible by 100 but does not validate whether it's an integer or not. I appreciate the help I have gotten so far. It's helping me to reach my epiphony.

i hope it could be help you in this matter.

Private Sub TextBox1_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Validated

Dim number As Double = 0

If Double.TryParse(Me.TextBox1.Text, number) Then

'your code


MessageBox.Show("Insert a number")


End If

End Sub

commented: This got me on the right track +1

Thanks Chuchaykaw. That is a great help. I have used similar to that in this code:

 Dim number As Double = 0

        If Integer.Parse(TextBox1.Text) Mod 100 <> 0 And Double.TryParse(Me.TextBox1.Text, number) Then
            Windows.Forms.MessageBox.Show("Please enter a valid number")
        Else : TextBox2.Focus()
        End If

However, it keeps giving me a format error: Input string was not in a correct format.

 Dim intvalue As Integer
        If Not Integer.TryParse(TextBox1.Text, intvalue) OrElse intvalue Mod 100 <> 0 Then
            MessageBox.Show("Please enter a Valid Number", "Message", MessageBoxButtons.OKCancel)

        End If

This is the code that works! Thanks for all the help. Just a simple if/then statement. This is an awesome forum for learning how to do vb.net!

Can I ask question to this thread?
how to validate the text box that needed to be the datatype was "DATE"?
Advance thanks for your response.