2012-04-12 4 views
1

안녕하세요. im은 전자 메일 주소를 입력했는지 확인하기 위해 텍스트 상자의 유효성을 검사하려고합니다. 다른 사람의 코드를 복사 한 다음 내 프로그램에 맞게 변경했습니다. 입력 유효한 이메일은 여전히 ​​사용Textbox 전자 메일 유효성 검사

Private Sub EmailTextBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EmailTextBox.Validating 
    Dim temp As String 
    temp = EmailTextBox.Text 
    Dim conditon As Boolean 
    emailaddresscheck(temp) 
    If emailaddresscheck(conditon) = False Then 
     MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry") 
     EmailTextBox.Text = "" 
     EmailTextBox.BackColor = Color.Blue 
    Else 
     EmailTextBox.BackColor = Color.Green 
    End If 

End Sub 


Private Function emailaddresscheck(ByVal emailaddress As String) As Boolean 
    Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" 
    Dim emailAddressMatch As Match = Regex.Match(emailaddress, pattern) 
    If emailAddressMatch.Success Then 
     emailaddresscheck = True 
    Else 
     emailaddresscheck = False 
    End If 
End Function 

Private Sub EmailTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmailTextBox.TextChanged 
    EmailTextBox.BackColor = Color.White 
    Dim temp As String 
    temp = EmailTextBox.Text 
    Dim conditon As Boolean 
    emailaddresscheck(temp) 
    : If emailaddresscheck(conditon) = True Then 
     MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry") 
     EmailTextBox.Text = "" 
     EmailTextBox.BackColor = Color.Yellow 
    Else 
     EmailTextBox.BackColor = Color.Green 
    End If 
End Sub 

색상은 녹색과 노란색했다하지만 난 식별하기 위해 상자의 색상을 변경 문제가 박스 오류 때문에 파란색 나타납니다 ..이었다 있었다 잘못된 이메일 항목을 말한다 .. .im이 코드 스 니핏 (snipit of code)에 있다고 가정합니다. 사전에

Private Function emailaddresscheck(ByVal emailaddress As String) As Boolean 
    Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" 
    Dim emailAddressMatch As Match = Regex.Match(emailaddress, pattern) 
    If emailAddressMatch.Success Then 
     emailaddresscheck = True 
    Else 
     emailaddresscheck = False 
    End If 
End Function 

감사합니다 .. :) X

+0

나는 정규 표현식을 좋아합니다. – Ben

답변

2

을 , 조금 변경했습니다

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 
     TextBox1.BackColor = Color.White 
     Dim temp As String 
     temp = TextBox1.Text 
     'Dim conditon As Boolean = False 
     If emailaddresscheck(temp) = True Then 
      ': If emailaddresscheck(conditon) = True Then 
      TextBox1.BackColor = Color.Green 
     Else 
      'MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry") 
      'TextBox1.Text = "" 
      TextBox1.BackColor = Color.Yellow 
     End If 

    End Sub 

나는 결코 허용하지 않을 메시지를 중단했습니다. 복사하여 붙여 넣지 않으면 올바른 이메일을 입력하십시오.

또한 그렇지 않으면 코드가 정확한지,이 패턴

Dim pattern As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
+0

코드에서 'emailaddresscheck (temp) = True Then'이면 '= True'가 중복됩니다. – Ben

4

이메일 주소가 유효한 그것에서 MailAddress 만드는 시도 할 수 있는지 확인하는 쉬운 방법 : 다음 코드를 확인

Try 
    Dim testAddress = New MailAddress(email) 
Catch ex As FormatException 
    ' not a valid email address 
End Try 
0

당신이 잘못된 상태 메시지 상자를주고있다보십시오. 아래와 같이 코딩을 변경하십시오. 참고로

If emailaddresscheck(conditon) = True Then 
      msgbox ("Correct email id") 
Else 
    MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry") 
    EmailTextBox.Text = "" 

End If 

:

Function EmailAddressCheck(ByVal emailAddress As String) As Boolean 

     Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$" 
     'Dim pattern As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
     Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern) 
     If emailAddressMatch.Success Then 
      EmailAddressCheck = True 
     Else 
      EmailAddressCheck = False 
     End If 

End Function 

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click 
     'EmailAddressCheck(TextBox6.Text) 
     If EmailAddressCheck(TextBox6.Text) = True Then 
      MessageBox.Show("Please enter your email address correctly", "Incorrect Email Entry") 
      TextBox6.Text = "" 
     Else 
      MsgBox("correct") 
     End If 
End Sub 
+0

plz 코드 스페이서에 모든 코드 예제를 넣으면 편집 모드에서 중괄호 ({})가 있습니다. – bresleveloper

0

P

rivate Function validateEmail(ByVal Email As String) 
     Try 
      Dim myEmails As String() = Email.Split(",") 
      Dim isV As Boolean = True 
      For i As Integer = 0 To myEmails.Length - 1 
       ' Validate One by Ibe 
       isV = Global.ValidateEmail.IsValidEmail(myEmails(i).ToString) 
       If (isV = False) Then 
        Return False 
       End If 
      Next 
      Return isV 
     Catch ex As Exception 
     End Try 
    End Function 
  1. 이 간단하고 쉽고, 문자열로 이메일 주소를 전달합니다. 부울을 반환합니다.
관련 문제