Solved

Custom Masked TextBox

Posted on 2008-10-12
7
1,831 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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