2009-08-25 9 views
29

मैं एक फ़ंक्शन का परीक्षण करना चाहता हूं कि एक स्ट्रिंग को ईमेल पते की तरह स्वरूपित किया गया हो।मैं .NET Framework के साथ ईमेल पता स्वरूपण को कैसे सत्यापित करूं?

ऐसा करने के लिए .NET ढांचे के साथ अंतर्निहित क्या है?

यह काम करता है:

Function IsValidEmailFormat(ByVal s As String) As Boolean 
    Try 
     Dim a As New System.Net.Mail.MailAddress(s) 
    Catch 
     Return False 
    End Try 
    Return True 
End Function 

लेकिन, वहाँ एक और अधिक सुरुचिपूर्ण तरीका है?

उत्तर

55

अपनी खुद की सत्यापन से परेशान न करें। .NET 4.0 ने MailAddress कक्षा के माध्यम से सत्यापन में काफी सुधार किया है। बस MailAddress address = new MailAddress(input) का उपयोग करें और यदि यह फेंकता है, तो यह मान्य नहीं है। यदि आरएफसी 2822 अनुपालन ईमेल पता spec के रूप में आपके इनपुट की कोई संभावित व्याख्या है, तो यह इसे इस तरह से पार्स करेगा। उपरोक्त regexes, यहां तक ​​कि एमएसडीएन लेख एक भी गलत हैं क्योंकि वे एक प्रदर्शन नाम, एक उद्धृत स्थानीय हिस्सा, डोमेन के लिए एक डोमेन शाब्दिक मूल्य, स्थानीय भाग के लिए सही डॉट-परमाणु विनिर्देशों को ध्यान में रखने में विफल रहते हैं, संभावना है कि मेल पता कोण ब्रैकेट में, डिस्प्ले नाम के लिए एकाधिक उद्धृत-स्ट्रिंग मान, बच निकले वर्ण, डिस्प्ले नाम, टिप्पणियों और अधिकतम मान्य मेल पता लंबाई में यूनिकोड हो सकता है। मैंने System.Net.Mail के लिए .NET 4.0 में मेल एड्रेस पार्सर को फिर से लिखने में तीन सप्ताह बिताए और मुझे भरोसा दिलाया, यह कुछ नियमित अभिव्यक्तियों के साथ आने से कहीं अधिक कठिन था क्योंकि बहुत सारे एज-केस हैं। .NET 4.0 बीटा 2 में MailAddress कक्षा में यह बेहतर कार्यक्षमता होगी।

एक और बात, केवल एक चीज जिसे आप मान्य कर सकते हैं वह मेल पता का प्रारूप है। आप कभी मान्य नहीं कर सकते कि एक ईमेल पता वास्तव में उस पते पर ईमेल भेजने के बिना ईमेल प्राप्त करने के लिए मान्य है और यह देखकर कि सर्वर इसे डिलीवरी के लिए स्वीकार करता है या नहीं। यह असंभव है और एसएमटीपी कमांड होने पर आप इसे मेल करने के लिए मेल सर्वर को दे सकते हैं, कई बार ये अक्षम हो जाएंगे या गलत परिणाम लौटाएंगे क्योंकि स्पैमर ईमेल पते खोजने के लिए यह एक आम तरीका है।

+1

जवाब देने के लिए धन्यवाद। मुझे यह सुनकर खुशी हो रही है कि इसे माइक्रोसॉफ्ट से कुछ ध्यान मिला है! लेकिन, बस स्पष्ट करने के लिए, आप कह रहे हैं कि सर्वोत्तम अभ्यास * अपवाद को पकड़ने के लिए है जैसा मैंने किया है उपर्युक्त समारोह में? –

+0

हां, आपको FormatException को पकड़ना होगा कि अगर पता अमान्य है तो यह फेंकता है। आपका कोड सही है। –

+0

एक आखिरी बात: System.Net.Mail ने .NET 4.0 के लिए माइक्रोसॉफ्ट से सभी प्रकार के ध्यान प्राप्त कर लिया है। आप देख सकते हैं कि हमने एनसीएल ब्लॉग पर अपनी पोस्ट में क्या सुधार किया है: http://blogs.msdn.com/ncl/archive/2009/08/06/what-s-new-in-system-net-mail .aspx –

1

आपको ईमेल पते मान्य करने के लिए Regular Expressions का उपयोग करना चाहिए।

+20

ताकि आप 2 समस्याओं :) – voyager

+3

इंटरनेट और इस साइट हो सकता है एक regex के साथ इस को हल करने में * गलत * प्रयास से भरा सदमे में हैं। जब तक आप एक रेगेक्स विज़ार्ड ** और ** एक ईमेल विज़ार्ड नहीं हैं, तब तक इसके बारे में भी मत सोचें। (मैं हूं, और मैंने किया, और फैसला नहीं किया।) – tripleee

0

आप ऐसा करने के लिए रेगेक्स का उपयोग कर सकते हैं।

इसके बारे में बहुत सारे लेख लिखे गए हैं; यह तब आया जब मैंने 'ईमेल पते को सत्यापित करने के लिए regex' के लिए Google की खोज की: Find or Validate an Email Address

+0

एक यात्री के साथ सहमत हैं। अब मुझे दो समस्याएं होंगी। –

+1

ये एमएसडीएन लेख में रेगेक्स से बेहतर हैं लेकिन अभी भी कई किनारे के मामलों में असफल हैं। –

5

MSDN अनुच्छेद: How to: Verify That Strings are in Valid E-Mail Format

यह उदाहरण विधि है कि स्ट्रिंग एक रेगुलर एक्सप्रेशन पैटर्न के अनुरूप सत्यापित करने के लिए Regex.IsMatch (स्ट्रिंग, स्ट्रिंग) विधि कहते हैं।

Function IsValidEmailFormat(ByVal s As String) As Boolean 
    Return Regex.IsMatch(s, "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$") 
End Function 
+0

यह निश्चित लगता है। लेकिन, मैं अभी भी System.Net.Mail के भीतर कहीं भी अंतर्निहित और गड़बड़ कर ऐसा फ़ंक्शन ढूंढना पसंद करूंगा। :( –

+10

यह ईमेल पते के साथ कई अलग-अलग किनारे के मामलों को ध्यान में रखता है, जिनमें से सबसे महत्वपूर्ण उद्धृत स्थानीय भाग है। ऐसे कई गैर-अल्फान्यूमेरिक वर्ण हैं जो मान्य हैं और साथ ही यह रेगेक्स भी अनुमति नहीं देता है मुझे पता है कि यह एक एमएसडीएन आलेख है लेकिन यह गलत है। मैंने System.Net.Mail.MailAddress में .NET 4.0 के लिए पता पार्सिंग को फिर से लिखा है और बहुत मजबूत हो और इन चीजों को ध्यान में रखो। अधिक जानकारी के लिए नीचे मेरा उत्तर देखें। –

+0

@ जेफ टकर यह [आरएफसी 822 अनुपालन regex] है (http://ex-parrot.com/~pdw/Mail-RFC822-Address.html) – user17753

2

आप पहली बार गलत प्रतीकों दर्ज करके उपयोगकर्ता को प्रतिबंधित करने के लिए है, तो आप इस पाठ बॉक्स कुंजी दबाने घटना का उपयोग कर

Private Sub txtemailid_KeyPress(ByVal sender As System.Object, 
           ByVal e As System.Windows.FormsKeyPressEventArgs) Handles txtemailid.KeyPress 

    Dim ac As String = "@" 
    If e.KeyChar <> ChrW(Keys.Back) Then 
     If Asc(e.KeyChar) < 97 Or Asc(e.KeyChar) > 122 Then 
      If Asc(e.KeyChar) <> 46 And Asc(e.KeyChar) <> 95 Then 
       If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then 
        If ac.IndexOf(e.KeyChar) = -1 Then 
         e.Handled = True 

        Else 

         If txtemailid.Text.Contains("@") And e.KeyChar = "@" Then 
          e.Handled = True 
         End If 

        End If 


       End If 
      End If 
     End If 

    End If 

End Sub 

ऊपर कोड केवल 0 9 (अंक के लिए इनपुट az करने के लिए उपयोगकर्ता (छोटे), की अनुमति देगा द्वारा कर सकते हैं), @ ,., _

और पाठ बॉक्स नियंत्रण से मान्य घटना का उपयोग कर ईमेल आईडी को मान्य करने के बाद नियमित अभिव्यक्ति

Private Sub txtemailid_Validating(ByVal sender As System.Object, 
            ByVal e As System.ComponentModel.CancelEventArgs) 
    Handles txtemailid.Validating 

    Dim pattern As String = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)[email protected][a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$" 


    Dim match As System.Text.RegularExpressions.Match = Regex.Match(txtemailid.Text.Trim(), pattern, RegexOptions.IgnoreCase) 
    If (match.Success) Then 
     MessageBox.Show("Success", "Checking") 
    Else 
     MessageBox.Show("Please enter a valid email id", "Checking") 
     txtemailid.Clear() 
    End If 
End Sub 
2
'----------------------------------------------------------------------- 

'Creater : Rachitha Madusanka 

'http://www.megazoon.com 

'[email protected] 

'[email protected] 

'Web Designer and Software Developer 

'@ http://www.zionx.net16.net 

'----------------------------------------------------------------------- 




Function ValidEmail(ByVal strCheck As String) As Boolean 
    Try 
     Dim bCK As Boolean 
     Dim strDomainType As String 


     Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< " 
     Dim i As Integer 

     'Check to see if there is a double quote 
     bCK = Not InStr(1, strCheck, Chr(34)) > 0 
     If Not bCK Then GoTo ExitFunction 

     'Check to see if there are consecutive dots 
     bCK = Not InStr(1, strCheck, "..") > 0 
     If Not bCK Then GoTo ExitFunction 

     ' Check for invalid characters. 
     If Len(strCheck) > Len(sInvalidChars) Then 
      For i = 1 To Len(sInvalidChars) 
       If InStr(strCheck, Mid(sInvalidChars, i, 1)) > 0 Then 
        bCK = False 
        GoTo ExitFunction 
       End If 
      Next 
     Else 
      For i = 1 To Len(strCheck) 
       If InStr(sInvalidChars, Mid(strCheck, i, 1)) > 0 Then 
        bCK = False 
        GoTo ExitFunction 
       End If 
      Next 
     End If 

     If InStr(1, strCheck, "@") > 1 Then 'Check for an @ symbol 
      bCK = Len(Left(strCheck, InStr(1, strCheck, "@") - 1)) > 0 
     Else 
      bCK = False 
     End If 
     If Not bCK Then GoTo ExitFunction 

     strCheck = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "@")) 
     bCK = Not InStr(1, strCheck, "@") > 0 'Check to see if there are too many @'s 
     If Not bCK Then GoTo ExitFunction 

     strDomainType = Right(strCheck, Len(strCheck) - InStr(1, strCheck, ".")) 
     bCK = Len(strDomainType) > 0 And InStr(1, strCheck, ".") < Len(strCheck) 
     If Not bCK Then GoTo ExitFunction 

     strCheck = Left(strCheck, Len(strCheck) - Len(strDomainType) - 1) 
     Do Until InStr(1, strCheck, ".") <= 1 
      If Len(strCheck) >= InStr(1, strCheck, ".") Then 
       strCheck = Left(strCheck, Len(strCheck) - (InStr(1, strCheck, ".") - 1)) 
      Else 
       bCK = False 
       GoTo ExitFunction 
      End If 
     Loop 
     If strCheck = "." Or Len(strCheck) = 0 Then bCK = False 

ExitFunction: 
     ValidEmail = bCK 
    Catch ex As ArgumentException 
     Return False 
    End Try 
    Return ValidEmail 
End Function 
2
Public Function ValidateEmail(ByVal strCheck As String) As Boolean 
     Try 
      Dim vEmailAddress As New System.Net.Mail.MailAddress(strCheck) 
     Catch ex As Exception 
      Return False 
     End Try 
     Return True 
    End Function 
+2

उत्तर को बेहतर समझने के लिए कोड स्निपेट के बजाय कुछ स्पष्टीकरण के बाद – user2720864

+0

कृपया अपने उत्तर में कुछ स्पष्टीकरण जोड़ें। – DontVoteMeDown

0

मैं मंजूरी दे दी 'जवाब' इस मामले में परीक्षण किया है का उपयोग करते हुए और यह नहीं है वास्तव में एक वैध ईमेल पता क्या है के विनिर्देशों का पालन करना प्रतीत होता है। कई सिरदर्दों के बाद मुझे यह रेगेक्स मिला जो माइक्रोसॉफ्ट की तुलना में एक बेहतर काम करता है।

"(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]" + 
")+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:" + 
"\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(" + 
"?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ " + 
"\t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\0" + 
"31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\" + 
"](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+" + 
"(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:" + 
"(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z" + 
"|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)" + 
"?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\" + 
"r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[" + 
" \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)" + 
"?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]" + 
")*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[" + 
" \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*" + 
")(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]" + 
")+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)" + 
"*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+" + 
"|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r" + 
"\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:" + 
"\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t" + 
"]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031" + 
"]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](" + 
"?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?" + 
":(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?" + 
":\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?" + 
":(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?" + 
"[ \t]))*""(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\"".\[\] " + 
"\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|" + 
"\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>" + 
"@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""" + 
"(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t]" + 
")*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" + 
""".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?" + 
":[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[" + 
"\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\"".\[\] \000-" + 
"\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(" + 
"?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;" + 
":\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([" + 
"^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\""" + 
".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\" + 
"]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\" + 
"[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\" + 
"r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] " + 
"\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]" + 
"|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"".\[\] \0" + 
"00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\" + 
".|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@," + 
";:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|""(?" + 
":[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*" + 
"(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\""." + 
"\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[" + 
"^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\]" + 
"]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(" + 
"?:(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" + 
""".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(" + 
"?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[" + 
"\[""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t" + 
"])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t" + 
"])+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?" + 
":\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|" + 
"\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:" + 
"[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\"".\[\" + 
"]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)" + 
"?[ \t])*(?:@(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""" + 
"()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)" + 
"?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>" + 
"@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[" + 
" \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@," + 
";:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]" + 
")*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\" + 
""".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?" + 
"(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[""()<>@,;:\\""." + 
"\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:" + 
"\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[" + 
"""()<>@,;:\\"".\[\]]))|""(?:[^\""\r\\]|\\.|(?:(?:\r\n)?[ \t]))*""(?:(?:\r\n)?[ \t])" + 
"*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])" + 
"+|\Z|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\" + 
".(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z" + 
"|(?=[\[""()<>@,;:\\"".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(" + 
"?:\r\n)?[ \t])*))*)?;\s*)" 

मैंने इसे पहले से ही एक सरल अनुप्रयोग का उपयोग करके एक वीबी स्ट्रिंग के रूप में स्वरूपित कर दिया है। समस्या के पूर्ण जवाब होने के बजाय बहुत खराब स्टैक ओवरफ़्लो 'कोडिंग रिपोजिटरी' होने में अधिक रुचि रखता है।

+5

मैं गरीब डेवलपर को दयनीय करता हूं, जिसने इस बीमियोथ की समस्या निवारण की है। –

-1
Public Shared Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean 
     If emailAddress.Length = 0 Then 
      errorMessage = "E-mail address is required." 
      Return False 
     End If 
     If emailAddress.IndexOf("@") > -1 Then 
      If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) AndAlso emailAddress.Split(".").Length > 0 AndAlso emailAddress.Split(".")(1) <> "" Then 
       errorMessage = "" 
       Return True 
      End If 
     End If 
     errorMessage = "E-mail address must be valid e-mail address format." 
     Return False 
    End Function 
+0

ईमेल सत्यापन के लिए बहुत कुछ है ... –

0

की तरह "पता @ स्थानीय होस्ट" और "[email protected]" ईमेल वास्तव में वैध पते दिए गए हैं और आप अपने खुद के ईमेल सर्वर (आमतौर पर साथ ही मेजबान फ़ाइल को संशोधित करके किया जाता है) चलाकर इन परीक्षण कर सकते हैं। एक पूर्ण समाधान के लिए, तथापि:

''' <summary> 
''' METHODS FOR SENDING AND VALIDATING EMAIL 
''' </summary> 
''' <remarks></remarks> 
Public Class email 

    ''' <summary> 
    ''' check if email format is valid 
    ''' </summary> 
    ''' <param name="emailAddress">[required] Email address.</param> 
    ''' <param name="disallowLocalDomain">[optional] Allow headers like "@localhost"?</param> 
    ''' <param name="allowAlerts">[optional] Enable error messages?</param> 
    ''' <returns>Returns true if email is valid and false otherwise.</returns> 
    ''' <remarks></remarks> 
    Public Shared Function isValid(ByVal emailAddress As String, 
            Optional ByVal disallowLocalDomain As Boolean = True, 
            Optional ByVal allowAlerts As Boolean = True 
            ) As Boolean 
     Try 
      Dim mailParts() As String = emailAddress.Split("@") 
      If mailParts.Length <> 2 Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "Your address is missing a header [i.e. ""@domain.tld""].", 
          MsgBoxStyle.Exclamation, "No Header Specified") 
       End If 
       Return False 
      End If 
      If mailParts(mailParts.GetLowerBound(0)) = "" Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "The username portion of the e-mail address you provided (before the @ symbol) is empty.", 
          MsgBoxStyle.Exclamation, "Invalid Email User") 
       End If 
       Return False 
      End If 
      Dim headerParts() As String = mailParts(mailParts.GetUpperBound(0)).Split(".") 
      If disallowLocalDomain AndAlso headerParts.Length < 2 Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "Although addresses formatted like [[email protected]] are valid, " & 
          "only addresses with headers like ""sample.org"", ""sample.com"", and etc. " & 
          "[i.e. @domain.org] are accepted.", 
          MsgBoxStyle.Exclamation, "Invalid Header") 
       End If 
       Return False 
      ElseIf headerParts(headerParts.GetLowerBound(0)) = "" Or 
        headerParts(headerParts.GetUpperBound(0)) = "" Then 
       If allowAlerts Then 
        MsgBox("Valid email addresses are formatted [[email protected]]. " & 
          "Your header """ & mailParts(mailParts.GetUpperBound(0)) & """ is invalid.", 
          MsgBoxStyle.Exclamation, "Invalid Header") 
       End If 
       Return False 
      End If 
      Dim address As MailAddress = New MailAddress(emailAddress) 
     Catch ex As Exception 
      If allowAlerts Then 
       MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Invalid Email Address") 
      End If 
      Return False 
     End Try 
     Return True 
    End Function 

End Class 'email' 
संबंधित मुद्दे