我使用vb.net在Visual Studio中有一个常规表单(不是Windows窗体)。一旦失去焦点,我想验证手机字段,在用户选择离开字段的意义上。我如何举办活动?丢失焦点事件不会导致下拉列表? 谢谢。 〜尼塔
答案 0 :(得分:1)
您可以使用以下方法验证电话号码。只需将电话号码参数传递给方法即可。
Protected Sub txtPhone_TextChanged(sender As Object, e As EventArgs) Handles txtPhone.TextChanged
If Not IsPhoneNumberValid(txtPhone.Text) Then
Dim isvalid = False
lblValidatioMessage.Visible = True
lblValidatioMessage.Text = "*Invalid Phonenumber"
Else
lblValidatioMessage.Visible = False
lblValidatioMessage.Text = ""
End If
End Sub
Private Shared Function IsPhoneNumberValid(phoneNumber As String) As Boolean
Dim result As String = ""
Dim chars As Char() = phoneNumber.ToCharArray()
For count = 0 To chars.GetLength(0) - 1
Dim tempChar As Char = chars(count)
If [Char].IsDigit(tempChar) Or "()+-., ".Contains(tempChar.ToString()) Then
result += StripNonAlphaNumeric(tempChar)
Else
Return False
End If
Next
Return result.Length = 10 'Length of US phone numbers is 10
End Function
Private Shared Function StripNonAlphaNumeric(value As String) As String
Dim regex = New Regex("[^0-9a-zA-Z]", RegexOptions.None)
Dim result As String = ""
If regex.IsMatch(value) Then
result = regex.Replace(value, "")
Else
result = value
End If
Return result
End Function
并在您的代码前面
<asp:Label ID="lblPhone" runat="server" Text="Phone"></asp:Label>
<p><asp:TextBox ID="txtPhone" runat="server" AutoPostBack="True"></asp:TextBox></p>
<asp:Label ID="lblValidatioMessage" Visible="False" runat="server" Text="" ForeColor="red"></asp:Label>
答案 1 :(得分:0)
尝试使用Control.Validating和Control.Validated事件。基本上,Validating事件是为验证而设计的。如果手机号码无效,请设置e.Cancel = True,焦点将保留在该字段上。
以下代码示例使用派生类TextBox并验证用户输入的电子邮件地址。如果电子邮件地址不是标准格式(包含“@”和“。”),则验证失败,将显示ErrorProvider图标,并取消事件。此示例要求在窗体上创建TextBox和ErrorProvider控件。 - Source
Private Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean
' Confirm there is text in the control.
If textBox1.Text.Length = 0 Then
errorMessage = "E-mail address is required."
Return False
End If
' Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
If emailAddress.IndexOf("@") > -1 Then
If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) Then
errorMessage = ""
Return True
End If
End If
errorMessage = "E-mail address must be valid e-mail address format." + ControlChars.Cr + _
"For example 'someone@example.com' "
Return False
End Function
Private Sub textBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles textBox1.Validating
Dim errorMsg As String
If Not ValidEmailAddress(textBox1.Text, errorMsg) Then
' Cancel the event and select the text to be corrected by the user.
e.Cancel = True
textBox1.Select(0, textBox1.Text.Length)
' Set the ErrorProvider error with the text to display.
Me.errorProvider1.SetError(textBox1, errorMsg)
End If
End Sub
Private Sub textBox1_Validated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles textBox1.Validated
' If all conditions have been met, clear the error provider of errors.
errorProvider1.SetError(textBox1, "")
End Sub
答案 2 :(得分:0)
Private Function IsPhoneNumberValid(ByVal Number As String) As Boolean
dim PhoneValid As boolean
Dim PhoneNumber As String = "^[1-9]\d{2}-[1-9]\d{2}-\d{4}$"
Dim ChekPhone As New Regex(PhoneNumber )
If Not String.IsNullOrEmpty(Number ) Then
PhoneValid = ChekPhone.IsMatch(Number )
Else
PhoneValid = False
End If
Return PhoneValid
End Function
Private Sub txtPhoneNumber_LostFocus(sender As Object, e As System.EventArgs) Handles txtPhoneNumber.LostFocus
If Not IsPhoneNumberValid(txtPhoneNumber.Text) Then
MessageBox.Show("Please Enter Phone Number!")
txtPhoneNumber.Clear()
txtPhoneNumber.Focus()
End If
End Sub