need a pattern to match a string which should NOT be --
777777777
888888888
999999999
or start with 00 or 02 or 04. Also the string should be 9 characters long.
when i tried to go create a pattern to match the above requirements, i got it done by -
Dim _pattern6 As String = "^(7+|8+|9+|(00|07|08|09|17|18|19|28|29|43|48|69|70|78|79|80|96|97).*)$"
could not get the NOT MATCH part done. to negate the condition -- i tried all types of [^] - but didnt work.
FroglegPosted Mar 9, 2011, 3:47 AM
Imports System.Text.RegularExpressions
Imports System
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If String.IsNullOrEmpty(TextBox1.Text) Then
Exit Sub
End If
Dim str As String = TextBox1.Text.Trim ' remove whitespaces
checkChar(str)
End Sub
Sub checkChar(ByVal s As String)
Dim _pattern6 As String = "^(7+|8+|9+|6+|5+|4+|3+|2+|1+|(00|07|08|09|17|18|19|28|29|43|48|69|70|78|79|80|96|97).*)$"
Dim m As Match = Regex.Match(s, _pattern6)
If m.Success Then
MessageBox.Show("Invalid characters")
Else
MessageBox.Show("Good to go")
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim allowedChars As String = "0123456789" 'numbers only allowed in textbox
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If
'In KeyPress event
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = String.Empty
TextBox1.Focus()
End Sub
End Class
FroglegPosted Mar 8, 2011, 12:53 PM
To check 9 characters use
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = TextBox1.Text.Trim ' remove whitespaces
Dim i As Integer = str.Length
If i > 9 Then ' if text longer than 9 chars
MessageBox.Show("Too many characters")
End If
End Sub
Do you want a numbers only textbox ?
The first character cannot be zero ?