Code for validating email in java

[email protected]"); emails.add("user'[email protected]"); //Invalid emails emails.add("@yahoo.com"); String regex = "^[a-z A-Z0-9_! `~^.-] @[a-z A-Z0-9.-] $"; Pattern pattern = Pattern.compile(regex); for(String email : emails) Output: [email protected]: true [email protected] : true [email protected]: true user? List emails = new Array List(); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); //Invalid emails emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); emails.add("[email protected]"); String regex = "^[\w!

^ #start of the line [_A-Za-z0-9-\ ] # must start with string in the bracket [ ], must contains one or more ( ) ( # start of group #1 \.[_A-Za-z0-9-] # follow by a dot "." and string in the bracket [ ], must contains one or more ( ) )* # end of group #1, this group is optional (*) @ # must contains a "@" symbol [A-Za-z0-9-] # follow by string in the bracket [ ], must contains one or more ( ) ( # start of group #2 - first level TLD checking \.[A-Za-z0-9] # follow by a dot "." and string in the bracket [ ], must contains one or more ( ) )* # end of group #2, this group is optional (*) ( # start of group #3 - second level TLD checking \.[A-Za-z] # follow by a dot "." and string in the bracket [ ], with minimum length of 2 ) # end of group #3 $ #end of the line The combination means, email address must start with “_A-Za-z0-9-\ ” , optional follow by “.[_A-Za-z0-9-]”, and end with a “@” symbol. Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected], true Email is valid : [email protected] , true Email is valid : [email protected], true Email is valid : [email protected] , true Email is valid : mkyong [email protected], true Email is valid : [email protected], true Email is valid : mkyong , false Email is valid : [email protected] , false Email is valid : [email protected] , false Email is valid : [email protected], false Email is valid : [email protected] , false Email is valid : [email protected], false Email is valid : mkyong()*@, false Email is valid : [email protected]%*, false Email is valid : [email protected], false Email is valid : [email protected], false Email is valid : [email protected]@, false Email is valid : [email protected] , false PASSED: Valid Email Test([

My main concern was the performence of the Get Username() method.

Is using the method within itself the best choice in this case?

Note: Your Reg Ex is not going to admit (a great many possible) e Mail addresses, and will admit invalid ones. domain names cannot have _, TLD's can be other than 3 letters, TLD's cannot be 3 digits, and the left-hand-side is significantly more forgiving than your regex.) I know this isn't related to the question you're asking, but do be aware that John_Crichton%special [email protected] (or .museum) is potentially valid while [email protected]_bar.999 is not.

If you look at the Wikipedia article for valid email address it supports a lot of special characters but to cover most of the valid email addresses, I am assuming an email address to consist of characters, digits and special characters , – , . Also every email address must contain only one @ symbol. So a subdomain can contain characters, digits and special characters – and _. Taking all these into account some valid email addresses are: Email ID [email protected] is valid? false Email ID journaldev()[email protected] is valid? false Email ID journaldev..[email protected] is valid? The email’s domain name must start with “A-Za-z0-9-“, follow by first level Tld (.com, .net) “.[A-Za-z0-9]” and optional follow by a second level Tld (au, my) “\.[A-Za-z]”, where second level Tld must start with a dot “.” and length must equal or more than 2 characters. Here’s a Java example to show you how to use regex to validate email address. [email protected], [email protected], [email protected] [email protected], [email protected], [email protected] 3. I'm Nataraja Gootooru, programmer by profession and passionate about technologies.

830

Leave a Reply

  1. Local girls to chat with for free no sign ups needed 22-Sep-2017 17:43

    Not everyone using online dating sites is looking for love. As if all that isn’t bad enough, romance scammers are now involving their victims in online bank fraud.

  2. dating disston 14-Oct-2017 17:46

    Tullaksesi elinikäiseksi premium (KULTA) -jäseneksi ja avataksesi tämän ominaisuuden, sinun tulee vain ostaa vapaavalintainen määrä poletteja kerran!

  3. catholic dating and relationships 05-Feb-2018 06:27

    Oh, and every last one of them is drop-dead beautiful.