Validating characters
-
Hi, Im using .Net 2, creating a user entry form. How do I prevent 1 or more characters from being entered into a textbox. For example: one textbox, I dont want a user entering slashes or single quotes. In another textbox, I dont want the user entering single quotes. Have I got to write a whole validation thing, or is there some sort of formatting or regex I can use? Any response gratefully appreciated. Mark
-
Hi, Im using .Net 2, creating a user entry form. How do I prevent 1 or more characters from being entered into a textbox. For example: one textbox, I dont want a user entering slashes or single quotes. In another textbox, I dont want the user entering single quotes. Have I got to write a whole validation thing, or is there some sort of formatting or regex I can use? Any response gratefully appreciated. Mark
Hi Mark You can use regular expressions as far as i know to do what you want or you could use a masked textbox in .Net 2.0. However to do what you want you need to you use the keypress event of a textbox and ignore character you dont want to allow by setting e.handled = true. Private Sub txtText1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtText1.KeyPress 'If the key pressed is ' or " or any type of punctuation mark then do not allow If e.KeyChar.IsPunctuation(e.KeyChar) Then e.Handled = True End If End Sub 'This will ignore a backslash If e.KeyChar = Microsoft.VisualBasic.Chr(Keys.OemBackslash) Then e.Handled = True End If Hope this Helps Keith