我希望我的用户只能以 937XXXXXXXX 阿富汗国际号码格式输入 ph#。
到目前为止,我已经编写了以下代码,但它并没有像我想要的那样工作:
Private Function Valid_Contact_No(ByVal Contact_No As String, ByRef errorMessage As String) As Boolean
Dim regex As New System.Text.RegularExpressions.Regex("^[937][0-9]{1,11}$")
' Confirm there is text in the control.
If txtContact_no.Text.Length = 0 Then
errorMessage = "Contact No# is required (allowed format: 937XXXXXXXX)"
Return False
End If
If txtContact_no.Text.Length > 11 Then
errorMessage = "Contact_No Can not be more than 11 digits (allowed format: 937XXXXXXXX)"
Return False
End If
If (regex.IsMatch(txtContact_no.Text)) Then
errorMessage = ""
Return True
End If
errorMessage = "A valid Contact No# is required" + ControlChars.Cr
Return False
End Function
任何帮助将不胜感激:)
最佳答案
937
代码应该在括号外:
"^937[0-9]{0,11}$|7[0-9]{0,9}$"
括号内的内容是字符集,但您需要 937
作为前缀。
您需要管道 (|)
符号来更改正则表达式。
至于字符串长度,匹配正则表达式两次,每次匹配检查字符串的长度。
关于regex - 阿富汗电话号码的正则表达式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373364/