Results 1 to 15 of 15

Thread: valid email address

  1. #1

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    valid email address

    I want to loop through my datatable rows and check for valid email addresses, if the address is valid, I want to add the row to the database, if not I want to add it to another datatable, but my main problem at the moment is checking for a valid email address, the code I have so far is

    VB Code:
    1. 'Loop through the rows of the DataTable
    2.         Do While i <= dt.Rows.Count - 1
    3.             checkEmail = dt.Rows(i).Item("E-Mail")
    4.             'If curCat = "" Then
    5.             If InStr(checkEmail, "@") = 1 Then
    6.                 'prevCat = curCat
    7.                 lblMessage.Text = "Email Checked"
    8.                 'Dim shRow As DataRow = dt.NewRow
    9.                
    10.                 'dt.Rows.InsertAt(shRow, i)
    11.  
    12.  
    13.                 i += 1
    14.             End If
    15.  
    16.             i += 1
    17.         Loop

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: valid email address

    You could look into regex (unless you're talking about authenticating that the address is real rather than the format)?

    Good article on this:
    http://www.4guysfromrolla.com/webtech/052899-1.shtml

    You won't need to use javascript for this, but you can make use of the pattern they provide. Just ad a regular expression control to your loop and handle the matches.
    Last edited by sevenhalo; Jun 26th, 2006 at 10:33 AM.

  3. #3

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: valid email address

    I am not bothered if it is real, just that it is in the correct format.

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: valid email address

    ok, without messing with your code too much, how does this work:
    VB Code:
    1. Dim regex As New System.Text.RegularExpressions.Regex("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$")
    2.         'Loop through the rows of the DataTable
    3.         Do While i <= dt.Rows.Count - 1
    4.             checkEmail = dt.Rows(i).Item("E-Mail")
    5.             'If curCat = "" Then
    6.             If regex.IsMatch(checkEmail) Then
    7.                 'Do whatever you need to do for an authenticated email
    8.                 i += 1
    9.             Else
    10.                 'Do whatever you need to do for a failed email
    11.             End If
    12.  
    13.             i += 1
    14.         Loop

  5. #5

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: valid email address

    Excellent, thanks very much,
    I was also wondering is it possible to check a cells value for things like double quotes or single quotes, and if so remove them???

    thanks

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: valid email address

    Yeah, deffinitely. There's string methods that you can use to find the indexof or remove. They return a value though, so you'll have to set the return value to the data type. IE:

    str = str.Replace("text", string.Empty)

  7. #7

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: valid email address

    Trying to run the code

    VB Code:
    1. Try
    2. Dim regex As New System.Text.RegularExpressions.Regex("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$")
    3.    
    4.             For Each datar In dt.Rows
    5.  
    6.             Next
    7.             Do While i <= dt.Rows.Count - 1
    8.                 checkEmail = dt.Rows(i).Item("E-Mail")
    9.                 'If curCat = "" Then
    10.                 If regex.IsMatch(checkEmail) Then
    11.                     'Do whatever you need to do for an authenticated email
    12.                     lblMessage.Text = "fine"
    13.                 Else
    14.                     lblmessage2.Text = "some problems"
    15.                     'Do whatever you need to do for a failed email
    16.                 End If
    17.  
    18.                 i += 1
    19.  
    20.             Loop
    21.         Catch ex As Exception
    22.             lblMessage.Text = ex.ToString
    23.         End Try

    But I keep getting
    "Argument Exception was unhandled by user code on the regular expression"

    Grateful for help
    Last edited by soluga; Jun 27th, 2006 at 05:50 AM.

  8. #8

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: valid email address

    used this instead

    "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

  9. #9
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: valid email address

    Actually, have a look at the two here:
    http://www.cambiaresearch.com/cambia...ail_regex.aspx

    I tested both the loose and strict pattern. Both seemed to work in the short trial I put them through.

  10. #10
    Addicted Member rabid lemming's Avatar
    Join Date
    Feb 2005
    Posts
    210

    Re: valid email address

    The only place to look for regular expressions has to be: http://regexlib.com/

    I will wait for death with a smile and a big stick

  11. #11

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: valid email address

    Did it this way in the end...

    VB Code:
    1. Const replacePattern As String = "[?:\/*""<>|]"
    2.         Const emailPattern As String = "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
    3.  
    4.         Dim replaceRegEx As New System.Text.RegularExpressions.Regex(replacePattern, System.Text.RegularExpressions.RegexOptions.Compiled)
    5.         Dim emailRegEx As New System.Text.RegularExpressions.Regex(emailPattern, System.Text.RegularExpressions.RegexOptions.Compiled)

    also yesterday I asked about filtering out unwanted characters!! I did this like ...

    VB Code:
    1. replaceRegEx.IsMatch(something) Then
    2. replaceRegEx.Replace("[?:\/*""<>|]", "")

    My problem now is, that even though I have removed the characters, they still show up in the new datatable!! but I will put that in a new thread.

    Thanks

  12. #12

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: valid email address

    more probs..

    I am passing the code

    VB Code:
    1. ElseIf replaceRegEx.IsMatch(organization) = True Then
    2.                 replaceRegEx.Replace("[?:\/*""<>|]", "")
    3.                 lblmessage2.Text = organization

    but although the label will show the text it has caught, it does not show the trimmed text !! so how can I tell, if the text is actually getting trimmed? was sure I had this working earlier today!!

  13. #13
    Addicted Member rabid lemming's Avatar
    Join Date
    Feb 2005
    Posts
    210

    Smile Re: valid email address

    Im sure you'd have to do is something like this wouldn't you ?

    Code:
    replaceRegEx = replaceRegEx.Replace("[?:\/*""<>|]", "")
    organization = replaceRegEx.toString.trim()
    lblmessage2.Text = organization
    Hoping that might help you

    I will wait for death with a smile and a big stick

  14. #14

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: valid email address

    I have got it working like this...

    VB Code:
    1. dtnRow("Organization") = Regex.Replace(organization, "[?:\/*""<>|]", "")

    however I want to be able to remove a single ' quotation as in O'Brien, but when I add a ' to the "[?:\/*""<>|]", "" then it throws everything out!!
    Last edited by soluga; Jun 28th, 2006 at 04:11 AM.

  15. #15
    Addicted Member rabid lemming's Avatar
    Join Date
    Feb 2005
    Posts
    210

    Question Re: valid email address

    Could you use another line to do that like:

    organization.ToString.Replace(Chr(34), "")


    I will wait for death with a smile and a big stick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width