我是.net编程的新手。我有一个产品供应商数据库,有一些供应商的详细信息。在phonetextbox上我希望电话号码至少为8位数。我编码了这个,但是没有用。你能帮我吗?
'Dim numbers As Decimal = Convert.ToDecimal(PhoneTextBox.Text)
Dim regex As Regex = New Regex("^\d{8}")
Dim match As Match = regex.Match(numbers)
If Not match.Success() Then
MessageBox.Show("Attention!!!The phone number must be at least 8 digits")
e.Cancel = True
End If
End Sub`
答案 0 :(得分:0)
这几乎可以完成你想要做的事情。正如评论中所说,不需要正则表达式进行长度检查,它绝对应该是字符串而不是数字类型。
Dim submission = "1234567"
If (submission.Length <= 8 ) Then
Console.WriteLine("Attention!!!The phone number must be at least 8 digits")
'e.Cancel = True
End If
答案 1 :(得分:0)
Sub Main()
Dim phonePattern As New Regex("^\d{3}-\d{7}$")
Dim phoneNumber As String = "210-1234567"
If (Not phonePattern.IsMatch(phoneNumber)) Then
Console.WriteLine("Number {0} is not in right format [999-9999999]", phoneNumber)
Else
Console.WriteLine("Number {0} is in right format [999-9999999]", phoneNumber)
End If
Console.ReadLine()
End Sub