|
-
Jun 26th, 2006, 09:57 AM
#1
Thread Starter
Addicted Member
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:
'Loop through the rows of the DataTable
Do While i <= dt.Rows.Count - 1
checkEmail = dt.Rows(i).Item("E-Mail")
'If curCat = "" Then
If InStr(checkEmail, "@") = 1 Then
'prevCat = curCat
lblMessage.Text = "Email Checked"
'Dim shRow As DataRow = dt.NewRow
'dt.Rows.InsertAt(shRow, i)
i += 1
End If
i += 1
Loop
-
Jun 26th, 2006, 10:26 AM
#2
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.
-
Jun 26th, 2006, 10:32 AM
#3
Thread Starter
Addicted Member
Re: valid email address
I am not bothered if it is real, just that it is in the correct format.
-
Jun 26th, 2006, 10:38 AM
#4
Re: valid email address
ok, without messing with your code too much, how does this work:
VB Code:
Dim regex As New System.Text.RegularExpressions.Regex("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$")
'Loop through the rows of the DataTable
Do While i <= dt.Rows.Count - 1
checkEmail = dt.Rows(i).Item("E-Mail")
'If curCat = "" Then
If regex.IsMatch(checkEmail) Then
'Do whatever you need to do for an authenticated email
i += 1
Else
'Do whatever you need to do for a failed email
End If
i += 1
Loop
-
Jun 26th, 2006, 12:10 PM
#5
Thread Starter
Addicted Member
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
-
Jun 26th, 2006, 12:20 PM
#6
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)
-
Jun 27th, 2006, 04:48 AM
#7
Thread Starter
Addicted Member
Re: valid email address
Trying to run the code
VB Code:
Try
Dim regex As New System.Text.RegularExpressions.Regex("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$")
For Each datar In dt.Rows
Next
Do While i <= dt.Rows.Count - 1
checkEmail = dt.Rows(i).Item("E-Mail")
'If curCat = "" Then
If regex.IsMatch(checkEmail) Then
'Do whatever you need to do for an authenticated email
lblMessage.Text = "fine"
Else
lblmessage2.Text = "some problems"
'Do whatever you need to do for a failed email
End If
i += 1
Loop
Catch ex As Exception
lblMessage.Text = ex.ToString
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.
-
Jun 27th, 2006, 06:52 AM
#8
Thread Starter
Addicted Member
Re: valid email address
used this instead
"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
-
Jun 27th, 2006, 07:11 AM
#9
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.
-
Jun 27th, 2006, 07:35 AM
#10
Addicted Member
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
-
Jun 27th, 2006, 08:57 AM
#11
Thread Starter
Addicted Member
Re: valid email address
Did it this way in the end...
VB Code:
Const replacePattern As String = "[?:\/*""<>|]"
Const emailPattern As String = "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Dim replaceRegEx As New System.Text.RegularExpressions.Regex(replacePattern, System.Text.RegularExpressions.RegexOptions.Compiled)
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:
replaceRegEx.IsMatch(something) Then
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
-
Jun 27th, 2006, 10:16 AM
#12
Thread Starter
Addicted Member
Re: valid email address
more probs..
I am passing the code
VB Code:
ElseIf replaceRegEx.IsMatch(organization) = True Then
replaceRegEx.Replace("[?:\/*""<>|]", "")
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!!
-
Jun 27th, 2006, 10:21 AM
#13
Addicted Member
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
-
Jun 28th, 2006, 03:54 AM
#14
Thread Starter
Addicted Member
Re: valid email address
I have got it working like this...
VB Code:
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.
-
Jun 28th, 2006, 05:50 AM
#15
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|