How To Create Validation On Textbox Using Visual Basic?

Asked By 100 points N/A Posted on -
qa-featured

I have a textbox that will accept the quantity of products ordered.

It should allow only numbers.

Thanks

SHARE
Best Answer by techgeek
Answered By 0 points N/A #87646

How To Create Validation On Textbox Using Visual Basic?

qa-featured

 

There are different ways to create validations on textbox. heres a simple one.
 
Often, to ensure that users enter only numbers in a text field,
you'll want to validate the text as they enter it. The textbox's
Change() event provides the best place to do so. However, simply
using the Is Numeric() function alone won't do the trick. For
instance, suppose you created the following procedure
private sub text1_change()
 
if not isnumeric(text1.text) then
 
text1.text=""
 
end if
Answered By 100 points N/A #87648

How To Create Validation On Textbox Using Visual Basic?

qa-featured

Your code does the job. But  if  the user input is not number, the whole text is erased. what I want is that the textbox will not allow other keys except numbers and still not remove previous numbers inputted by the user.

Thanks

Best Answer
Best Answer
Answered By 0 points N/A #87649

How To Create Validation On Textbox Using Visual Basic?

qa-featured

Aside from the is numeric function, you can also use the keypress. follow this code and give it a try

Private Sub Text1_KeyPress(KeyAscii as Integer) 

'Ignore negatives 
If KeyAscii = 45 then goto EndProc 
'If Key pressed is not numeric then make key Null 
If (KeyAscii < 48) or (Keyascii >57) then KeyAscii = 0 
Endproc: 
End Sub 

Answered By 0 points N/A #87650

How To Create Validation On Textbox Using Visual Basic?

qa-featured

That's a nice idea Mr Techgeek.. That textbox will look more professional using your code..

Answered By 100 points N/A #87651

How To Create Validation On Textbox Using Visual Basic?

qa-featured

Thanks for the knowledge you’ve shared with me. It can help a lot for my project. thanks again.

Answered By 0 points N/A #87652

How To Create Validation On Textbox Using Visual Basic?

qa-featured

You're always welcome.. feel free to ask here in techyv if you have problems.

A lot of expert here will help you..

Login/Register to Answer

Related Questions