Solved

Custom Masked TextBox

Posted on 2008-10-12
7
1,834 Views
Last Modified: 2012-05-05
Hi, I'm using VB 2005, WinForms. I would like to create a custom Masked Textbox for an email address. We all know that a legitimate email address needs to contain an ampersand (@) and a period (.), but the rest is unformatable to some extent. How would one format such a masked textbox?  

Ultimately, my intent is to make sure that the data that my user enters into this field is indeed a legitimate email address. Is there a better way?

Thanks,
Fulano
0
Comment
Question by:Mr_Fulano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 15

Accepted Solution

by:
JackOfPH earned 350 total points
ID: 22699875
0
 

Author Comment

by:Mr_Fulano
ID: 22699940
Thanks, that gives me a lot to work with, but the thread never answered the following questions that was asked by Ozo:

>> "That returns False for this valid email address:
The Fred and Barney Comedy Team <fred&barney@stonehenge.com>" <<

I tested the email address offered by Ozo and he's right, it returns False, when in fact, it should be True. I also tested "thisismyemail@mycompany.comcomcomcomcom" and that returned True instead of False.

Any thoughts on how to tweek this?

Thanks,
Fulano
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 22700649
Try this code

 ' The following pattern checks whether the input string is a valid email
        ' address in the form "name@domain.com". Actually, it does not have to be a
        ' ".com" address. Any combination of letters following the last period are
        ' fine. Also, the email name can have a dash or be separated by one or more
        ' periods. The Domain name can also have multiple words separated by periods.

        ' [\w-]+    
        ' One or more matches of any character (a-z, A-Z, 0-9, and underscore) or dash. On either side of the @ character this ensures the address is in the form name@domainname.
        ' \.              
        ' An escaped period. (Without the backslash, a period matches any single character except the newline character.) Using this ensures there is at least one period in the domain name.
        ' *?            
        ' A non-greedy, or minimal, match of zero or more matches of the preceding expression.
        ' ([\w-]+\.)*?    
        ' Combination of the three preceding expressions:
        ' Zero or more non-greedy matches of the expression one or more matches of any character (a-z, A-Z, 0-9, and underscore) or dash, followed by only one period.

        If Not Regex.IsMatch(txtEmail.Text, _
            "^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$") Then

            txtEmail.ForeColor = Color.Red
            isValid = False
        Else
            txtEmail.ForeColor = Color.Black
        End If

        If isValid Then
            lblValid.Visible = True
        Else
            lblValid.Visible = False
        End If
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 75 total points
ID: 22700968
Hi Fulano,
I never had problems with that email because I never found one link that. Most of the emails doesn't allot the "&" (that I know) and I use know this mask "^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*\s+<(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})>$|^(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,6})$"
Don't forget that you have other emails like abc@def.info or abc@def.museum
If you don't like this regex masks you can implement your own validation.
You can also try this ones:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=0&categoryId=1 
0
 
LVL 2

Assisted Solution

by:kewlchap_85
kewlchap_85 earned 75 total points
ID: 22701532
Hi, check the code below. It is personally tested to be very true and working. just pass the email address to be  validated to the function and it will return "1" if its correct or else return "0". Import the following class

Imports System.Text.RegularExpressions

Public Function validate_email(ByVal em As String)
        Dim regex_email As Regex = New Regex("^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")
 
        If regex_email.IsMatch(em) Then
            Return 1
        Else
            Return 0
        End If
    End Function

Open in new window

0
 

Author Comment

by:Mr_Fulano
ID: 22709053
Wow, thank you all for your help. RegEx is certainly something I'd like to learn more about an I have some material that I'll be studying in the coming days...good "rainy day" reading.

I appreciate the help and hope the point distribution is fair.

Thanks,
Fulano
0
 
LVL 2

Expert Comment

by:kewlchap_85
ID: 22709072
Glad I Could help... Its really nice to understand the regex..
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

626 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