Click here to Skip to main content
14,692,470 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:

Hi,

I have textbox that takes only a fully characters string for example person name. If the user enter something like (paul123) or (1234), it should get an invalid input.

I try this:

Integer.parse(me.nametextbox.text)
msgbox("invalid")
me.nametextbox.lostfocus

It works when the user types an integer first, it gives the message but user types a character first it goes through - that is not what I want.

Any idea?

Best Regards

Posted
Updated 25-Aug-09 11:33am
v2

It's possible that the test is being interpreted as a hexadecimal number. Instead of using TryParse to determine if it's a good number, use a regular expression instead.

   
Try This Private Sub me.nametextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles me.nametextbox.KeyPress If (Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar))) Then e.Handled = True : MsgBox("Please enter valid Char.", MsgBoxStyle.Information, "Message") End If End Sub
   
v2
"[0-9]"
That regular expression will find any digit in a string. So, what you want to do is check the string to see if that regular expression returns any results. If it does, that means the string contains a digit. I don't want to make it too easy for you (as I expect you to do the learning and only come to us when you are running into a problem you can't seem to solve), so I will leave figuring out how to use the regular expression up to you. This should get you started: .Net Regular Expressions.

Alternatively, you could just loop through each character in the string and check if it is a digit. Again, that should be extremely simple, so I'll leave that as an exercise for you to do.
   
Thanks,

Can You provide me with an example of relax expression and how to apply?

Thanks again
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900