Solved

Custom Masked TextBox

Posted on 2008-10-12
7
1,822 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
Comment Utility
0
 

Author Comment

by:Mr_Fulano
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad I Could help... Its really nice to understand the regex..
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

10 Experts available now in Live!

Get 1:1 Help Now