Validation problem

filleke

New member
Joined
Jan 18, 2006
Messages
4
Location
Belgium
Programming Experience
1-3
Hello,

i want to create a validation rule but i can't seem to get it to work.

I need to validate the text inside my textbox. The format has to be 10 x 10
So, i need to control that i have digit digit space x space digit digit.

Does anyone know how i can do this?

This is what i use now but i keep getting eroors when i don't enter the right format.

Anyone an other solution?

VB.NET:
Private Sub txtRuimteVoorScherm_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRuimteVoorScherm.Leave
        Dim strRuimteArray() As String = Split(txtRuimteVoorScherm.Text, " ")
        If txtRuimteVoorScherm.Text = Nothing Then
            errpZaal.SetError(Me.txtRuimteVoorScherm, "U moet een juiste waarde ingeven")
        Else
            errpZaal.SetError(Me.txtRuimteVoorScherm, "")
            If Not IsNumeric(strRuimteArray(0)) And Not strRuimteArray(1) = "x" And Not IsNumeric(strRuimteArray(2)) Then
                errpZaal.SetError(Me.txtRuimteVoorScherm, "U moet een juiste waarde ingeven")
            Else
                errpZaal.SetError(Me.txtRuimteVoorScherm, "")
            End If
        End If
    End Sub
 
Just use the regular expression:

before your write the code please Imports System.Text.RegularExpressions

VB.NET:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        If Not Regex.Match(TextBox1.Text, "^\d+\s{1}x\s{1}\d+$").Success Then
            MessageBox.Show("Error Input")
        Else
            MessageBox.Show("success")
        End If
    End Sub
or

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim input() As String
        Dim spaceSeperator As Char() = New Char() {" "c}
        TextBox1.Text = Trim(TextBox1.Text)
        input = TextBox1.Text.Split(spaceSeperator)
        If input.Length > 3 or input.length < 1 Then
            MessageBox.Show("Error")
            Return
        Else
            If (Not IsNumeric(input(0))) Or (Not IsNumeric(input(2))) Or (input(1) <> "x") Then
                MessageBox.Show("Error")
                Return
            End If
        End If
        MessageBox.Show("success")       
End Sub
But i recommend you to use the regular expressions.
 
Last edited:
Back
Top