Açıklama Yok

FLFormatHelper.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // FLFormatHelper.m
  3. // FirstLink
  4. //
  5. // Created by unicode on 14-10-9.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "FLFormatHelper.h"
  9. @implementation FLFormatHelper
  10. + (BOOL)checkMobileFormat:(NSString *)mobile error:(NSError**)error
  11. {
  12. if (!mobile || [mobile length] == 0) {
  13. if (error != NULL) {
  14. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  15. code:0
  16. userInfo:@{NSLocalizedDescriptionKey:@"手机号码不能为空"}];
  17. }
  18. return NO;
  19. }
  20. NSString *mobileRegex = @"[1][0-9]{10}";
  21. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileRegex];
  22. BOOL matches = [predicate evaluateWithObject:mobile];
  23. if (!matches) {
  24. if (error != NULL) {
  25. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  26. code:0
  27. userInfo:@{NSLocalizedDescriptionKey:@"手机号码格式不正确"}];
  28. }
  29. }
  30. return matches;
  31. }
  32. + (BOOL)checkNicknameFormat:(NSString *)nickname error:(NSError **)error
  33. {
  34. if (!nickname || [nickname length] < 2 || [nickname length] > 12) {
  35. if (error != NULL) {
  36. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  37. code:0
  38. userInfo:@{NSLocalizedDescriptionKey:@"昵称长度为2-12位"}];
  39. }
  40. return NO;
  41. }
  42. if ([nickname rangeOfString:@" "].location != NSNotFound) {
  43. if (error != NULL) {
  44. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  45. code:0
  46. userInfo:@{NSLocalizedDescriptionKey:@"昵称不能含有空格"}];
  47. }
  48. return NO;
  49. }
  50. //一般用4E00-9FA5已经可以,如果要更广,则用2E80-A4CF || F900-FAFF || FE30-FE4F
  51. NSString *nicknameRegex = @"^(?!_)(?!.*?_$)[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]{2,12}$";
  52. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nicknameRegex];
  53. BOOL match = [predicate evaluateWithObject:nickname];
  54. if (!match) {
  55. if (error != NULL) {
  56. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  57. code:0
  58. userInfo:@{NSLocalizedDescriptionKey:@"昵称不合法"}];
  59. }
  60. }
  61. return match;
  62. }
  63. + (BOOL)checkPasswordFormat:(NSString *)password error:(NSError *__autoreleasing *)error {
  64. if (!password || [password length] == 0) {
  65. if (error != NULL) {
  66. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  67. code:0
  68. userInfo:@{NSLocalizedDescriptionKey:@"密码不能为空"}];
  69. }
  70. return NO;
  71. }
  72. NSString *passwordRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
  73. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
  74. BOOL match = [predicate evaluateWithObject:password];
  75. if (!match) {
  76. if (error != NULL) {
  77. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  78. code:0
  79. userInfo:@{NSLocalizedDescriptionKey:@"密码为6-16位且包含数字和字母组合"}];
  80. }
  81. }
  82. return match;
  83. }
  84. + (BOOL)checkVerifyCodeFormat:(NSString *)verifyCode error:(NSError**)error
  85. {
  86. if (!verifyCode || [verifyCode length] == 0) {
  87. if (error != NULL) {
  88. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  89. code:0
  90. userInfo:@{NSLocalizedDescriptionKey:@"验证码不能为空"}];
  91. }
  92. return NO;
  93. }
  94. NSString *verifyCodeRegex = @"[0-9]{6}";
  95. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", verifyCodeRegex];
  96. BOOL match = [predicate evaluateWithObject:verifyCode];
  97. if (!match) {
  98. if (error != NULL) {
  99. *error = [NSError errorWithDomain:@"FirstLinkErrorDomain"
  100. code:0
  101. userInfo:@{NSLocalizedDescriptionKey:@"验证码不正确"}];
  102. }
  103. }
  104. return match;
  105. }
  106. @end