Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Custom Masked TextBox

Posted on 2008-10-12
7
Medium Priority
?
1,842 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 1400 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
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 48

Assisted Solution

by:jpaulino
jpaulino earned 300 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 300 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

Independent Software Vendors: 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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

926 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