我想在不使用正則運算式的情況下驗證 vb6 中的國際電話號碼
只允許 () 此字符和數字,并且應小于 20 位。
我試過的:
private sub txtPhoneNo_validate ( cancel As boolean)
if txtPhoneNo = "" then exit sub
if not Isnumeric(txtPhoneNo) then
msgbox "phone number shold be in numeric only"
Elseif len(txtPhoneNo) > 20
msgbox "phone number shold be in numeric only"
Exit sub
end if
end sub
我想在我的代碼中添加用于驗證的國際格式和 () 這是只允許的字符
主要是我想驗證這種數字 44 779-345666 44 (0)779345666 0779 345666 0208-3456667
uj5u.com熱心網友回復:
根據您提出的要求,我建議的方法是強制用戶僅輸入有效字符:
Option Explicit
Private Sub Form_Load()
txtPhoneNo.MaxLength = 20
End Sub
Private Sub txtPhoneNo_KeyPress(KeyAscii As Integer)
'allow only <backspace> <space> ( ) - numeric
If Not (KeyAscii = 8 Or KeyAscii = 32 Or KeyAscii = 40 Or KeyAscii = 41 Or _
KeyAscii = 43 Or KeyAscii = 45 Or (KeyAscii >= 48 And KeyAscii <= 57)) Then
KeyAscii = 0
End If
End Sub
總之,此代碼強制最大長度為 20 個字符,如果鍵不是有效字符,則丟棄擊鍵。
這可能足以滿足您的需求,但實際上它并不能真正驗證資料。例如, 可以在文本框中的任何位置輸入,并且仍然通過我們的驗證。換句話說,我們并沒有完全執行國際電話號碼的規則。這樣做需要更多的代碼或使用正則運算式。
uj5u.com熱心網友回復:
以下答案確保包含不超過“20 位”,而不僅僅是字符。也保證了“ ”只能在前導位置,“-”和“”不能在前導位置。雖然其他解決方案可能是有效的,但如果您想保留該_Validate()事件,或者如果您想要除基本檢查之外的其他檢查,例如將“ ”限制為僅開頭,這可能是有用的。
Private Sub txtPhoneNo_Validate(ByRef Cancel As Boolean)
Const MaxLength As Long = 20
Dim ANI As String, Cleaned As String
ANI = txtPhoneNo.Text
If ANI = "" Then Exit Sub
Dim I As Long, C As String
For I = 1 To Len(ANI)
C = Mid(ANI, I, 1)
If IsNumeric(C) Then
Cleaned = Cleaned & C
ElseIf C = "(" Or C = ")" Or (I <> 1 And (C = " " Or C = "-")) Or (I = 1 And C = " ") Then
' don't count these in count
Else
MsgBox "Phone Number should be contain only numbers, parenthesis, a leading plus, and spaces."
Cancel = True
Exit Sub
End If
Next
If Len(Cleaned) > MaxLength Then
MsgBox "Phone Number should contain at most 20 digits (provided: " & Len(Cleaned) & ")."
Cancel = True
Exit Sub
End If
End Sub
uj5u.com熱心網友回復:
使用VB6中的正則運算式。國際電話號碼有很多正則運算式模式。使用他人完成和驗證的作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/488890.html
上一篇:特定格式的日期/時間驗證
下一篇:如何在嵌套表單陣列中添加驗證?
