Blog : validate a textbox in vb.net

validate a textbox in vb.net

You can check the text string, i.e., textbox1.text, to make sure it has nothing besides alphabet characters in the .Leave event. This will catch an error when the user tabs to the next control, for example. You can do this using a regular expression (import System.Text.RegularExpressions for this example), or you can check the text "manually."

<code>PrivateSub TextBox1_Leave(ByVal sender AsObject,ByVal e As System.EventArgs)Handles TextBox1.Leave
  IfNot Regex.Match(TextBox1.Text,"^[a-z]*

PrivateSub TextBox1_Leave(ByVal sender AsObject,ByVal e As System.EventArgs)Handles TextBox1.Leave
  IfNot Regex.Match(TextBox1.Text,"^[a-z]*$", RegexOptions.IgnoreCase).Success Then
  MsgBox("Please enter alpha text only.")
  TextBox1.Focus()EndIfEndSub

quot;, RegexOptions.IgnoreCase).Success Then
  MsgBox("Please enter alpha text only.")
  TextBox1.Focus()EndIfEndSub</code>