Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Regex to validate a date

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 13-02-2010
Member
 
Join Date: Nov 2009
Posts: 344
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"
Reply With Quote
  #3  
Old 13-02-2010
Member
 
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
}
Reply With Quote
  #4  
Old 13-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
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.
Reply With Quote
  #5  
Old 13-02-2010
Member
 
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;
}
Reply With Quote
  #6  
Old 13-02-2010
Member
 
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"));
	}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Regex to validate a date"
Thread Thread Starter Forum Replies Last Post
PHP REGEX to Validate Domain Name Xmen Software Development 6 13-05-2010 05:17 PM
Regex with xml tag Vodka Software Development 5 04-02-2010 02:11 AM
How to Validate XML Against a DTD? PsYcHo 1 Software Development 5 02-02-2010 12:33 AM
Query in php regex Linux-Us Software Development 5 14-12-2009 12:54 PM
WM 11 won't validate Buckwheat Media Player 3 02-03-2007 04:43 AM


All times are GMT +5.5. The time now is 07:36 AM.