Results 1 to 3 of 3
  1. #1
    valery is offline Member
    Join Date
    Jul 2007
    Posts
    32
    Rep Power
    0

    Default how to validate an email with pattern object

    I'm trying to validate an email with pattern object.
    and it validates only 'xxxxx@'
    I want that validates all 'xxx@zzz.yy'
    the code is the following one:
    Java Code:
      
    public static boolean validateEmail(String ca) {
            Pattern pattern= Pattern.compile("[a-zA-Z0-9_]+[.[a-zA-Z0-9]+]*@[a-zA-Z0-9_]+[.[a-zA-Z]+]+");
            Matcher en= pattern.matcher(ca);
            return !en.matches();
        }
    what's the problem?
    what am I doing wrong?

  2. #2
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    search on google. there's tons of code that does this.

  3. #3
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by valery View Post
    I'm trying to validate an email with pattern object.
    and it validates only 'xxxxx@'
    I want that validates all 'xxx@zzz.yy'
    the code is the following one:
    Java Code:
      
    public static boolean validateEmail(String ca) {
            Pattern pattern= Pattern.compile("[a-zA-Z0-9_]+[[B][COLOR="Red"].[/COLOR][/B][a-zA-Z0-9]+]*@[a-zA-Z0-9_]+[.[a-zA-Z]+]+");
            Matcher en= pattern.matcher(ca);
            return !en.matches();
        }
    what's the problem?
    what am I doing wrong?
    From a quick glance I would say you have forgotton to escape the '.' character. In regex the . defines any character so to explicitly search for just a . you need \. instead.
    Also remember as this is in a string the \ character is a java string escape character so you actually need to escape that as well.

    So basically replace . with \\. and that will get you going again.

    Whenever I am doing regex I always keep a cheat sheet to hand which I found on Web design, development and marketing - ILoveJackDaniels.com

    Hope this helps.

Similar Threads

  1. How to validate date using SampleDateFormat
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-04-2008, 02:38 PM
  2. repaint validate doLayout
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 10-27-2007, 06:53 PM
  3. How to validate date using SampleDateFormat
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:31 PM
  4. Replies: 3
    Last Post: 08-01-2007, 05:15 PM
  5. validate hex character
    By paul in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •