Is there some standard email validator code sample for Java ME or BlackBerry?
Asked
Active
Viewed 2,160 times
3 Answers
8
public static boolean validateEmailID(String email) {
email = email.trim();
String reverse = new StringBuffer(email).reverse().toString();
if (email == null || email.length() == 0 || email.indexOf("@") == -1) {
return false;
}
int emailLength = email.length();
int atPosition = email.indexOf("@");
int atDot = reverse.indexOf(".");
String beforeAt = email.substring(0, atPosition);
String afterAt = email.substring(atPosition + 1, emailLength);
if (beforeAt.length() == 0 || afterAt.length() == 0) {
return false;
}
for (int i = 0; email.length() - 1 > i; i++) {
char i1 = email.charAt(i);
char i2 = email.charAt(i + 1);
if (i1 == '.' && i2 == '.') {
return false;
}
}
if (email.charAt(atPosition - 1) == '.' || email.charAt(0) == '.' || email.charAt(atPosition + 1) == '.' || afterAt.indexOf("@") != -1 || atDot < 2) {
return false;
}
return true;
}

surya
- 96
- 1
- 1
5
Use this code for checking the given email id is valid or not,
private static boolean validateEmailID(String email) {
if (email == null || email.length() == 0 || email.indexOf("@") == -1 || email.indexOf(" ") != -1) {
return false;
}
int emailLenght = email.length();
int atPosition = email.indexOf("@");
String beforeAt = email.substring(0, atPosition);
String afterAt = email.substring(atPosition + 1, emailLenght);
if (beforeAt.length() == 0 || afterAt.length() == 0) {
return false;
}
if (email.charAt(atPosition - 1) == '.') {
return false;
}
if (email.charAt(atPosition + 1) == '.') {
return false;
}
if (afterAt.indexOf(".") == -1) {
return false;
}
char dotCh = 0;
for (int i = 0; i < afterAt.length(); i++) {
char ch = afterAt.charAt(i);
if ((ch == 0x2e) && (ch == dotCh)) {
return false;
}
dotCh = ch;
}
if (afterAt.indexOf("@") != -1) {
return false;
}
int ind = 0;
do {
int newInd = afterAt.indexOf(".", ind + 1);
if (newInd == ind || newInd == -1) {
String prefix = afterAt.substring(ind + 1);
if (prefix.length() > 1 && prefix.length() < 6) {
break;
} else {
return false;
}
} else {
ind = newInd;
}
} while (true);
dotCh = 0;
for (int i = 0; i < beforeAt.length(); i++) {
char ch = beforeAt.charAt(i);
if (!((ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a)
|| (ch == 0x2e) || (ch == 0x2d) || (ch == 0x5f))) {
return false;
}
if ((ch == 0x2e) && (ch == dotCh)) {
return false;
}
dotCh = ch;
}
return true;
}

bharath
- 14,283
- 16
- 57
- 95
-
Why the `email.indexOf(" ") != -1` test is needed ? ( at the first test ) – Nov 04 '11 at 13:46
-
2because an email can not have spaces – frayab Nov 04 '11 at 13:52
-
1this validation code is completely off: e.g. it dos not allow a '+' (0x2b) before the '@' sign although it is valid... [wikipedia](http://en.wikipedia.org/wiki/Email_address) gives a more thorough summary of what is allowed. the only official and authoritative source grammar for an email address is the [RFC](http://tools.ietf.org/html/rfc2822). and i wish any developer down there had read this RFC before writing such an horrendous code. – Adrien Plisson Jan 02 '12 at 10:24
-
The code serves the purpose and is not "horrendous" or "completely off"! For purposes of user registration you wont want email addresses with the + sign and other "valid" email addresses by the RFC that are not the norm. – Ajibola Jan 06 '12 at 05:09
-1
You can simply google around for email validation regex pattern. Thats the easiest and efficient way to check email string format. see the link below.
http://www.zparacha.com/ultimate-java-regular-expression-to-validate-email-address/
http://leshazlewood.com/2006/02/04/java-email-address-validation-the-right-way-regular-expression/
You will find many other examples for regex. For Regex, check the following link.

Parth
- 1,281
- 8
- 17