?
Solved

creating an function to get emails from an file like regex

Posted on 2011-10-10
9
Medium Priority
?
277 Views
Last Modified: 2012-05-12
hi i have an textbox where the user can type the emails there, but sometimes you know how users are, they don't follow the rules of programming. so they can put like

email1@hotmail.com email2.hotmail.com email4@gmail.com       or
email1@hotmail.com,email2.hotmail.com,email4@gmail.com       or
email1@hotmail.com;email2.hotmail.com;email4@gmail.com       or etc.

so i want an function that reads the textbox and returns an list of the emails like this

Function reademails(byval vtext as string) as datatable
begin
      find all emails inside and return the emails inside the datatable?
end

any help?
0
Comment
Question by:rafaelrgl
  • 5
  • 4
9 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36944231
>email2.hotmail.com
This can also be

emailpart1.emailpart2.hotmail.com

How would you know which dot to replace with @?

Why not enforce users to enter email in correct syntax?
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 36944380
ok, how we do that. so we enforce the user to type the right emails, since will be more than one somethimes.

so he can do that:

email1@hotmail.com       or

email1@hotmail.com;email2@hotmail.com     etc


but if we have this function i talked above would be perfect. what about the function take the emails that have this case you show me out of the result, and get the others ones.

what do you think?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36944530
I use following code which works both with one email and with multiple emails


If txtEmail.Text.Length > 0 Then
                If txtEmail.Text.Contains(";") Then
                    Dim emails() As String = txtEmail.Text.Split(";")
                    For Each email As String In emails
                        email = email.Trim
                        If System.Text.RegularExpressions.Regex.IsMatch(email, "^[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]$") = False Then
                            MessageBox.Show("One of the specified emails: (" & email & ") is not in a valid form. Please provide valid email addresses.", "Company Details", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            txtEmail.Focus()
                            Exit Sub
                        End If
                    Next
                Else
                    If System.Text.RegularExpressions.Regex.IsMatch(txtEmail.Text, "^[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]$") = False Then
                        MessageBox.Show("The specified email is not in a valid form. Please provide a valid email address.", "Company Details", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        txtEmail.Focus()
                        Exit Sub
                    End If
                End If
            End If

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:rafaelrgl
ID: 36953217
it works perfect, but when it's only one email that was typed, then it still say it's error because it's expecting the ; even when it's one email. is there a way to correct that?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36959174
I have the following condition

>If txtEmail.Text.Contains(";") Then

which makes sure that it deals with single and multiple emails separately.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 36959378
it's not working then, because if you type this:

fasdf@hotmail.com

it will say it's error, but it's not. it's just one email so does not need the ;

:(

i think this regex is not right:
If System.Text.RegularExpressions.Regex.IsMatch(txtEmail.Text, "^[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]$") = False Then
 
because it's saying the email i typed is wrong
any idea?
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 36959387
sorry, this is right. i got my error. thanks a lots for the help.
0
 
LVL 1

Author Closing Comment

by:rafaelrgl
ID: 36959388
thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36965375
Glad to help :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question