Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class EmailValidator {
  4.  
  5. public static boolean isValid(final char[] input) {
  6. if (input == null) {
  7. return false;
  8. }
  9.  
  10. int state = 0;
  11. char ch;
  12. int index = 0;
  13. int mark = 0;
  14. String local = null;
  15. ArrayList<String> domain = new ArrayList<String>();
  16.  
  17. while (index <= input.length && state != -1) {
  18. // Dealing with a char instead of char[] makes life a lot easier!
  19. if (index == input.length) {
  20. ch = '\0'; // We need to encode the end by using a terminator
  21. }
  22. else {
  23. ch = input[index];
  24. if (ch == '\0') {
  25. // but the terminator may not be part of the input!
  26. return false;
  27. }
  28. }
  29.  
  30. switch (state) {
  31.  
  32. case 0: {
  33. // Transition on {atext}
  34. if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
  35. || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-'
  36. || ch == '+') {
  37. state = 1;
  38. break;
  39. }
  40. // Unexpected Character -> Error state
  41. state = -1;
  42. break;
  43. }
  44.  
  45. case 1: {
  46. // Consume {atext}
  47. if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
  48. || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-'
  49. || ch == '+') {
  50. break;
  51. }
  52. if (ch == '.') {
  53. state = 2;
  54. break;
  55. }
  56. if (ch == '@') { // Endof local part
  57. local = new String(input, 0, index - mark);
  58. mark = index + 1;
  59. state = 3;
  60. break;
  61. }
  62. // Unexpected Character -> Error state
  63. state = -1;
  64. break;
  65. }
  66.  
  67. case 2: {
  68. // Transition on {atext}
  69. if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
  70. || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-'
  71. || ch == '+') {
  72. state = 1;
  73. break;
  74. }
  75. // Unexpected Character -> Error state
  76. state = -1;
  77. break;
  78. }
  79.  
  80. case 3: {
  81. // Transition on {alnum}
  82. if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
  83. || (ch >= 'A' && ch <= 'Z')) {
  84. state = 4;
  85. break;
  86. }
  87. // Unexpected Character -> Error state
  88. state = -1;
  89. break;
  90. }
  91.  
  92. case 4: {
  93. // Consume {alnum}
  94. if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
  95. || (ch >= 'A' && ch <= 'Z')) {
  96. break;
  97. }
  98. if (ch == '-') {
  99. state = 5;
  100. break;
  101. }
  102. if (ch == '.') {
  103. domain.add(new String(input, mark, index - mark));
  104. mark = index + 1;
  105. state = 5;
  106. break;
  107. }
  108. // Match EOL
  109. if (ch == '\0') {
  110. domain.add(new String(input, mark, index - mark));
  111. state = 6;
  112. break; // EOL -> Finish
  113. }
  114. // Unexpected Character -> Error state
  115. state = -1;
  116. break;
  117. }
  118.  
  119. case 5: {
  120. if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
  121. || (ch >= 'A' && ch <= 'Z')) {
  122. state = 4;
  123. break;
  124. }
  125. if (ch == '-') {
  126. break;
  127. }
  128. // Unexpected Character -> Error state
  129. state = -1;
  130. break;
  131. }
  132.  
  133. case 6: {
  134. // Success! (we don't really get here, though)
  135. break;
  136. }
  137. }
  138. index++;
  139. }
  140.  
  141. // Sanity checks
  142.  
  143. // Input not accepted
  144. if (state != 6)
  145. return false;
  146.  
  147. // Require at least a second level domain
  148. if (domain.size() < 2)
  149. return false;
  150.  
  151. // RFC 5321 limits the length of the local part
  152. if (local.length() > 64)
  153. return false;
  154.  
  155. // RFC 5321 limits the total length of an address
  156. if (input.length > 254)
  157. return false;
  158.  
  159. // TLD must only consist of letters and be at least two characters long.
  160. index = input.length - 1;
  161. while (index > 0) {
  162. ch = input[index];
  163. if (ch == '.' && input.length - index > 2) {
  164. return true;
  165. }
  166. if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
  167. return false;
  168. }
  169. index--;
  170. }
  171.  
  172. return true;
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement