Berikut adalah method untuk mengecek apakah email tersebut valid atau tidak, jika nilainya true maka valid dan sebaliknya.
public boolean isValidEmailAddress(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;
}
for (int i = 0; i < afterAt.length(); i++) {
char ch = afterAt.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;
}
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;
}
if(email.startsWith(".")){
return false;
}
int lastIndex = email.lastIndexOf('.');
String lastToken = email.substring(lastIndex + 1);
if (lastToken.length() >= 2) {
if (lastToken.length() <= 3){
//return true;
}else{
return false;
}
}else{
return false;
}
return true;
}
No comments:
Post a Comment
Silahkan Isi komentar anda,thanks!