Question

I have few regular expression validators for few textboxes in asp.net application written in vb.net.

Public Const NAME_REGEX As String = "[^<>""]*$"

It works fine for most user input if they type with keyboards however, if they copy a name from MS Word, the regex cannot detect those. For example,

John “Smith”

How can I deny users enter double quotes like that?

Was it helpful?

Solution

You can perform an automatic replacement (in the TextChanged Event of the TextBoxes, as shown below; or just before dealing with the given String). For example:

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
     TextBox1.Text = TextBox1.Text.Replace(ChrW(8220), "")
     TextBox1.Text = TextBox1.Text.Replace(ChrW(8221), "")
     TextBox1.Text = TextBox1.Text.Replace("""", "")
     If (TextBox1.TextLength > 0) Then
         TextBox1.Select(TextBox1.TextLength, 1)
     End If
End Sub

This code does not allow users to write ", or . The reason for a so complicated code is that dealing with "curvy" double quotes (as the ones in your sample code) is not straightforward: VB.NET considers them as normal double quotes in most of the situations, but this does not happen immediately (for example, while dealing with the inputs in the code above) and thus you have to consider all the eventualities. Here you have an interested link on this matter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top