none
Validate Emails before putting them in SQL Server RRS feed

  • Question

  • Hi,

    i want to import an xlsx file into a datagridview and then save it in SQL server.... but before i save it, i want to check if all emails are valid or not, and just save the records with valid emails. like, it needs to be at least:        (1 character)@(3 characters).(2 characters)

    but still, it can be like this: a@abc.def.ghi.com          having the domain and sub-domains

    this is my code so far, can someone please help me?




    • Edited by RchlE Monday, June 6, 2016 9:49 AM
    Tuesday, May 31, 2016 10:48 AM

All replies

  • You need to use a regular expression to validate an email address.  Here is an example.  I got the regex pattern on the internet and it seems very reliable.  You can Bing for other patterns if you want (regex email)

    Function IsValidEmail(email As String) As Boolean Dim regEx As Object Set regEx = CreateObject("vbscript.regexp") regEx.Global = True regEx.IgnoreCase = True regEx.Pattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$" Set regExp_Matches = regEx.Execute(email) If regExp_Matches.Count = 1 Then IsValidEmail = True End If
    Set regEx = Nothing
    End Function Sub test() If IsValidEmail("ABC@xyz.com") Then MsgBox ("This string is a valid email address.") End If End Sub



    • Edited by mogulman52 Tuesday, May 31, 2016 12:28 PM
    Tuesday, May 31, 2016 12:15 PM
  • Thanks a lot, i'm gonna see if it works :)
    Tuesday, May 31, 2016 2:04 PM