Solved

Custom Masked TextBox

Posted on 2008-10-12
7
1,824 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now