1,105,320 Community Members

Textbox Validation

Member Avatar
smitty68503
Light Poster
33 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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??

Thanks

Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
2
 

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.")
        MaskedTextBox1.Focus()
    End If
End Sub
Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

Hi,
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)

Member Avatar
smitty68503
Light Poster
33 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Thanks

Member Avatar
smitty68503
Light Poster
33 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Jamblaster
Junior Poster in Training
55 posts since Aug 2011
Reputation Points: 15 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

Hi,

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

If Isnumeric(MyInput) then
    'Do something
else
    Msgbox("Non Numeric input detected")
end if
Member Avatar
ddanbe
Posting Expert
5,335 posts since Oct 2008
Reputation Points: 2,054 [?]
Q&As Helped to Solve: 904 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

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.

Member Avatar
smitty68503
Light Poster
33 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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.

chuchaykaw
Newbie Poster
8 posts since Nov 2013
Reputation Points: 1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
1
 

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

Else

MessageBox.Show("Insert a number")

Me.TextBox1.Clear()

End If

End Sub

Member Avatar
smitty68503
Light Poster
33 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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")
            TextBox1.Focus()
        Else : TextBox2.Focus()
        End If

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

Member Avatar
smitty68503
Light Poster
33 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
 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)
            TextBox1.Focus()
            TextBox1.Clear()

        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!

Question Answered as of 5 Months Ago by G_Waddell, tinstaafl, Jamblaster and 2 others
Member Avatar
raidenx44
Newbie Poster
8 posts since Nov 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article