Results 1 to 6 of 6

Thread: Regex to validate a date

  1. #1
    Join Date
    Dec 2009
    Posts
    211

    Regex to validate a date

    Hello,
    I have stuck with a regular expression pattern in java. The aim is to validate email addresses. I have tried searching on internet but what I could not find helpful information and compiled so far do not give good results in both cases following
    Here is my code:
    Code:
    Pattern p =.compiles("(\\w +) @ (\\w +\\.)(\\w +) (\\.\\w +) * ");
    Matcher m = p.matcher(input);
    if (! m.find()){
      System.out.System.out.println("Email address has wrong format! Expected: xxx@xxx.xx (. Xx)>");
    }else{
      System.out.System.out.println("OK");
    }
    I am trying to find a regular expression for validating a date.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Regex to validate a date

    Hi,
    You used the wrong class Matcher : Method find () finds the first sub-string that matches your pattern. To verify that the entire string matches the pattern we must use the method matches (). Then he would perhaps see your pattern ... accept emails underscore and dash, as the points. The following emails are valid but not with your expressions:
    Here is the code.
    Code:
    "firstnm.lastname @ test.com"
    "example@test.com"
    "example@test.com"

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Regex to validate a date

    Hello,
    Do not use the method find() Which seeks an instance of the regular expression but the method games() Which it checks the consistency right.
    You can even use the direct method games() The String class (this method is that of the Matcher class but gives the same result).
    Just look at the following code.
    Code:
    Pattern p =.compiles("\\w + @\\w +\\.\\w + (\\.\\w +) * ");
    Matcher m = p.matcher(input);
    if(m.games() {
        / / OK
    } else {
        / / Invalid Email
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Regex to validate a date

    Hello,
    I think you can also use the following code instead of what you are using in the code.
    Code:
    if(in.games("\\w + @\\w +\\.\\w + (\\.\\w +) * ")) {
        / / OK
    } else {
        / / Invalid Email
    }
    Just try using the following code in your code and see if it works for you.

  5. #5
    Join Date
    Dec 2009
    Posts
    211

    Re: Regex to validate a date

    Hello,
    This is my update code, please have a look at it, if you think that there is a correction to me made in the code then please guide me with the same.
    Code:
    Public static boolean isEmailWellFormed(String email){
      String in = email;
    
      if (in == null){
        System.out.System.out.println("Email address can not be null!");
        return false;
      }
    	      
      / / regex pattern info: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
      / / checks for email right format
      Pattern Pattern p =.compiles("(\\w +) ((\\.|\\-)\\w +)*@((\\w +\\.)|(\\w +\\-))(\\w +) (\\.\\w +) * ");
      
      Matcher m = p.matcher(in);
      if (! m.games()) {
        System.out.System.out.println("Email address is not correct!");
        return false;
      }
     
      return true;
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Regex to validate a date

    Hello,
    For the Junit class you can use the following, if in case you are using it in your code.
    Code:
    @ Test
    	Public void emailIsNull(){
    		assertFalse(Validator.isEmailWellFormed(null));
    	}
    	
    	@ Test
    	Public void emailIsEmpty(){
    		assertFalse(Validator.isEmailWellFormed(""));
    	}
    	
    	@ Test
    	Public void emailStartsWithDot(){
    		assertFalse(Validator.isEmailWellFormed(. @ test.com "));
    	}
    	
    	@ Test
    	Public void emailStartsWithArobase(){
    		assertFalse(Validator.isEmailWellFormed("@ test.com"));
    	}
    	
    	@ Test
    	Public void emailWithoutArobase(){
    		assertFalse(Validator.isEmailWellFormed("test.com"));
    	}
    	
    	@ Test
    	Public void emailContains2Arobases(){
    		assertFalse(Validator.isEmailWellFormed("test @ test@test.com"));
    	}
    	
    	@ Test
    	Public void emailContainsIllegalChar(){
    		char c = ']';
    assertFalse(Validator.isEmailWellFormed("test" C + + "test @ test."));
    	}
    	
    	@ Test
    	Public void emailBadFormat1(){
    		assertFalse(Validator.isEmailWellFormed("test @ test"));
    	}
    	
    	@ Test
    	Public void emailBadFormat2(){
    		assertFalse(Validator.isEmailWellFormed("test@test.com test@test.com"));
    	}
    	
    	@ Test
    	Public void emrgfrm1(){
    		assertTrue(Validator.isEmailWellFormed("test@test.com"));
    	}
    	
    	@ Test
    	Public void emrgfrm2(){
    		assertTrue(Validator.isEmailWellFormed("test_test@test.com"));
    	}
    	
    	@ Test
    	Public void emrgfrm3(){
    		assertTrue(Validator.isEmailWellFormed("test.test @ test.com"));
    	}
    	
    	@ Test
    	Public void emrgfrm4(){
    		assertTrue(Validator.isEmailWellFormed("test-test@test.com"));
    	}
    	
    	@ Test
    	Public void emrgfrm5(){
    		assertTrue(Validator.isEmailWellFormed("test@test_test.ch"));
    	}
    
    @ Test
    	Public void emrgfrm6(){
    		assertTrue(Validator.isEmailWellFormed("test@test.test.ch"));
    	}
    	
    	@ Test
    	Public void emrgfrm7(){
    		assertTrue(Validator.isEmailWellFormed("test@test-test.ch"));
    	}

Similar Threads

  1. PHP REGEX to Validate Domain Name
    By Xmen in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 04:17 PM
  2. How to validate page in ASP.NET
    By MaggieK in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 01:56 AM
  3. How to Validate Email by using PHP?
    By NGV BalaKrishna in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 02:43 AM
  4. How to Validate XML Against a DTD?
    By PsYcHo 1 in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 12:33 AM
  5. How to validate windows XP
    By austin26 in forum Operating Systems
    Replies: 3
    Last Post: 18-11-2009, 05:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,726,953,426.20508 seconds with 17 queries