Determine if the phone number and price entered are legal

Source: Internet
Author: User
Tags vars

  1. Validity judgment of mobile phone number
  2. Detect if it is a mobile phone number
  3. -(BOOL) Ismobilenumber: (nsstring *) mobilenum
  4. {
  5. /** 
  6. * Mobile phone number
  7. * Mobile: 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
  8. * Unicom: 130,131,132,152,155,156,185,186
  9. * Telecom: 133,1349,153,180,189
  10. */
  11. NSString * MOBILE = @ "^1 (3[0-9]|5[0-35-9]|8[025-9]) \\d{8}$";
  12. /** 
  13. 10 * Mobile: China Mobile
  14. 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
  15. 12 */
  16. NSString * CM = @ "^1 (34[0-8]| (  3[5-9]|5[017-9]|8[278] \\d) \\d{7}$ ";
  17. /** 
  18. 15 * Unicom: China Unicom
  19. 16 * 130,131,132,152,155,156,185,186
  20. 17 */
  21. NSString * CU = @ "^1 (3[0-2]|5[256]|8[56]) \\d{8}$";
  22. /** 
  23. 20 * China Telecom: Telecom
  24. 21 * 133,1349,153,180,189
  25. 22 */
  26. NSString * CT = @ "^1 ((33|53|8[09)) [0-9]|349] \\d{7}$";
  27. /** 
  28. 25 * Mainland China fixed and PHS
  29. 26 * Area code: 010,020,021,022,023,024,025,027,028,029
  30. 27 * Number: seven-bit or eight-bit
  31. 28 */
  32. //NSString * PHS = @ "^0 (10|2[0-5789]|\\d{3}) \\d{7,8}$";
  33. nspredicate *regextestmobile = [nspredicate predicatewithformat:@ "Self MATCHES%@", MOBILE];
  34. nspredicate *REGEXTESTCM = [nspredicate predicatewithformat:@ "Self MATCHES%@", CM];
  35. nspredicate *REGEXTESTCU = [nspredicate predicatewithformat:@ "Self MATCHES%@", CU];
  36. nspredicate *REGEXTESTCT = [nspredicate predicatewithformat:@ "Self MATCHES%@", CT];
  37. if ([Regextestmobile evaluatewithobject:mobilenum] = = YES)
  38. || ([regextestcm evaluatewithobject:mobilenum] = = YES)
  39. || ([REGEXTESTCT evaluatewithobject:mobilenum] = = YES)
  40. || ([Regextestcu evaluatewithobject:mobilenum] = = YES))
  41. {
  42. return YES;
  43. }
  44. Else
  45. {
  46. return NO;
  47. }
  48. }
  49. Restricted input of special characters, validity judgment of price amount
  50. #define Mydotnumbers @ "0123456789.\n"
  51. #define Mynumbers @ "0123456789\n"
  52. -(void) createtextfiled {
  53. TextField1_ = [[Uitextfield alloc] initwithframe:cgrectmake (0, 0, 20, 20)];
  54. TextField1_. Delegate = self ;
  55. [self addsubview:textfield1_];
  56. TextField2_ = [[Uitextfield alloc] initwithframe:cgrectmake (0, 0, 20, 20)];
  57. TextField2_. Delegate = self ;
  58. [self addsubview:textfield2_];
  59. TextField3_ = [[Uitextfield alloc] initwithframe:cgrectmake (0, 0, 20, 20)];
  60. TextField3_. Delegate = self ;
  61. [self addsubview:textfield3_];
  62. }
  63. -(void) Showmymessage: (nsstring*) Ainfo {
  64. Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "hint" message:ainfo Delegate:   Self cancelbuttontitle:@ "OK" otherbuttontitles: nil, nil nil];
  65. [Alertview show];
  66. [Alertview release];
  67. }
  68. -(BOOL) TextField: (Uitextfield *) TextField shouldchangecharactersinrange: (nsrange) Range Replacementstring: (NSString *) string {
  69. Nscharacterset *cs;
  70. if ([TextField Isequal:textfield1_]) {
  71. CS = [[Nscharacterset charactersetwithcharactersinstring:mynumbers] Invertedset];
  72. nsstring *filtered = [[string Componentsseparatedbycharactersinset:cs] componentsjoinedbystring:@ ""  ];
  73. BOOL basictest = [string isequaltostring:filtered];
  74. if (!basictest) {
  75. [self showmymessage:@ "Only enter numbers"];
  76. return NO;
  77. }
  78. }
  79. Else if ([TextField Isequal:textfield2_]) {
  80. CS = [[Nscharacterset charactersetwithcharactersinstring:mynumbers] Invertedset];
  81. nsstring *filtered = [[string Componentsseparatedbycharactersinset:cs] componentsjoinedbystring:@ ""  ];
  82. BOOL basictest = [string isequaltostring:filtered];
  83. if (!basictest) {
  84. [self showmymessage:@ "Only enter numbers"];
  85. return NO;
  86. }
  87. }
  88. Else if ([TextField Isequal:textfield3_]) {
  89. Nsuinteger Ndotloc = [TextField. Text rangeofstring:@ "."]  . Location;
  90. if (Nsnotfound = = Ndotloc && 0! = Range) {
  91. CS = [[Nscharacterset charactersetwithcharactersinstring:mydotnumbers] Invertedset];
  92. }
  93. else {
  94. CS = [[Nscharacterset charactersetwithcharactersinstring:mynumbers] Invertedset];
  95. }
  96. nsstring *filtered = [[string Componentsseparatedbycharactersinset:cs] componentsjoinedbystring:@ ""  ];
  97. BOOL basictest = [string isequaltostring:filtered];
  98. if (!basictest) {
  99. [self showmymessage:@ "can only enter numbers and decimal points"];
  100. return NO;
  101. }
  102. if (nsnotfound! = Ndotloc && range. location > Ndotloc + 3) {
  103. [self showmymessage:@ "up to three digits after decimal point"];
  104. return NO;
  105. }
  106. }
  107. return YES;

Determine if the phone number and price entered are legal

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.