Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking valid format of url and email 2

Status
Not open for further replies.

dekcool

Programmer
Oct 2, 2002
231
PH
hi good day!

anyone have idea on how i can check if the string format of url or email address is correct only the format not if the url and the email existing just the format checking

thanks in advance!

____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
Sure, try this code.

Code:
String url ="[URL unfurl="true"]http://www.blogeasy.com";[/URL]
String email = "lucifer@hell.com";
Pattern urlPattern = Pattern.compile("(?:https?|ftp)://[^\\s,<>\\?]+?\\.[^\\s,<>\\?]+");
Matcher m = urlPattern.matcher(url);
if (m.find()) {
  // This is a valid url
} else {
  // Not a valid url
}
Pattern emailPattern = Pattern.compile("[^\\s,<>]+@[^\\s<>_]+\\.[^\\s<>_]+");
m = emailPattern.matcher(email);
if (m.find()) {
  // This is a valid email
} else {
  // Not a valid email
}

That should be pretty close. You can look up the RFC standard to make sure all the bad characters are excluded.

 
thank you,

is pattern and matcher are built in with java?


____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
again thanks,

iam new with java please tell me how i can implements those function, iam getting error.

____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
thanks!

____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
// i have this code why iam getting an error, please check thanks again


import java.util.regex.*;
class WebUtils {

public static boolean isValidEmail(String sEmailAdd) {
Pattern ePattern = Pattern.compile("[^\\s,<>]+@[^\\s<>_]+\\.[^\\s<>_]+");
Matcher match = ePattern.matcher(sEmailAdd);

if (match.find() == true) {
return true;
} else {
return false;
}
}
}



class EmailValidator {

public static void main(String[] args) {

if (WebUtils.isValidEmail(args[0]) == true) {
System.out.println(args[0] + " is a valid email address.");
} else {
System.out.println(args[0] + " is *not* a valid email address.");
}

}
}



____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
it works for me.

If you would be so nice to tell us, which error you get?
Compiletime or runtime?
If runtime, which testcase?
How is it shown?
Code-Line and message.

And use code-tags.
You put [ code] and [ /code] around your code, (without the space, which I must use, to make them visible.
btw:
Code:
  if (match.find() == true) {
         return true;
      } else {
       return false;
      }
is equivalent to
Code:
  return match.find();

seeking a job as java-programmer in Berlin:
 
thanks !

____________________________________________________
Invest your time in learning, Not just practicing.

DEK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top