Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include "InputValidator.h"
  2.  
  3. bool InputValidator::isUserNameValid(string userName)
  4. {
  5. if (userName.length() >= 3) {
  6. for each (char c in userName)
  7. {
  8. if (!islower(c) && !isupper(c) && !isdigit(c) && c != '_') {
  9. return false;
  10. }
  11. }
  12. return true;
  13. }
  14. return false;
  15. }
  16.  
  17. bool InputValidator::isPasswordValid(string password)
  18. {
  19. bool haveLower = false;
  20. bool haveUpper = false;
  21. bool haveSpecial = false;
  22. bool haveDigit = false;
  23.  
  24. if (password.length() >= 5) {
  25. for each (char c in password)
  26. {
  27. if (islower(c)) {
  28. haveLower = true;
  29. }
  30. else if (isupper(c)) {
  31. haveUpper = true;
  32. }
  33. else if (isdigit(c)) {
  34. haveDigit = true;
  35. }
  36. else if (c == '!' || c == '@' || c == '#' || c == '$') {
  37. haveSpecial = true;
  38. }
  39. else {
  40. return false;
  41. }
  42. }
  43. if (haveDigit && haveLower && haveUpper && haveSpecial) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49.  
  50. bool InputValidator::isEmailValid(string email)
  51. {
  52. int atPosition = email.find('@', 3);
  53. int dotPosition = email.find('.');
  54. if (atPosition < 0 || dotPosition < 0 || (dotPosition - atPosition > 6) || (dotPosition - atPosition < 2)) {
  55. return false;
  56. }
  57.  
  58. for (int i = 0; i < atPosition; i++)
  59. {
  60. if (!islower(email[i]) && !isupper(email[i]) && !isdigit(email[i]) && email[i] != '_') {
  61. return false;
  62. }
  63. }
  64.  
  65. for (int i = atPosition + 1; i < dotPosition; i++)
  66. {
  67. if (!islower(email[i])) {
  68. return false;
  69. }
  70. }
  71.  
  72. string domain = email.substr(dotPosition + 1, email.length() - dotPosition);
  73.  
  74. if (domain != "com" && domain != "bg" && domain != "net" && domain != "en") {
  75. return false;
  76. }
  77.  
  78. return true;
  79. }
  80.  
  81. bool InputValidator::isRealNameValid(string name)
  82. {
  83. if (name.empty()) {
  84. return true;
  85. }
  86.  
  87. int firstSpacePosition = name.find_first_of(' ', 1);
  88. int secondSpacePosition = name.find_first_of(' ', firstSpacePosition + 1);
  89.  
  90. if (name.length() > 255 || firstSpacePosition < 0 || secondSpacePosition < 0) {
  91. return false;
  92. }
  93.  
  94. string firstName = name.substr(0, firstSpacePosition);
  95. string secondName = name.substr(firstSpacePosition + 1, secondSpacePosition - firstSpacePosition - 1);
  96. string thirdName = name.substr(secondSpacePosition + 1, name.length() - secondSpacePosition);
  97.  
  98. for each (char c in firstName)
  99. {
  100. if (!islower(c) && !isupper(c)) {
  101. return false;
  102. }
  103. }
  104.  
  105. for each (char c in secondName)
  106. {
  107. if (!islower(c) && !isupper(c)) {
  108. return false;
  109. }
  110. }
  111.  
  112. for each (char c in thirdName)
  113. {
  114. if (!islower(c) && !isupper(c)) {
  115. return false;
  116. }
  117. }
  118.  
  119. return true;
  120. }
  121.  
  122. bool InputValidator::isAddressValid(string address)
  123. {
  124. if (address.empty()) {
  125. return true;
  126. }
  127.  
  128. if (address.length() > 255) {
  129. return false;
  130. }
  131.  
  132. for each (char c in address)
  133. {
  134. if (!islower(c) && !isupper(c) && !isdigit(c) && c != ' ') {
  135. return false;
  136. }
  137. }
  138.  
  139. return true;
  140. }
  141.  
  142. bool InputValidator::isBirthdateValid(string birthDate)
  143. {
  144. if (birthDate.empty()) {
  145. return true;
  146. }
  147.  
  148. int firstDashPosition = birthDate.find_first_of('-');
  149. int secondDashPosition = birthDate.find_first_of('-', firstDashPosition + 1);
  150.  
  151. if (birthDate.length() != 10 || firstDashPosition != 4 || secondDashPosition != 7) {
  152. return false;
  153. }
  154.  
  155. for (int i = 0; i < birthDate.length(); i++)
  156. {
  157. if (i == firstDashPosition || i == secondDashPosition) {
  158. continue;
  159. }
  160.  
  161. if (!isdigit(birthDate[i])) {
  162. return false;
  163. }
  164. }
  165.  
  166. return true;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement