IOS: A regular expression that determines whether a user name starts with a letter and the phone number is entered correctly

Source: Internet
Author: User

Create a new string Category: NSString (Check), defining class methods is more convenient

. h file

@interface nsstring (Check) // Verify user name input must start with a letter + (BOOL) Checkusername: (NSString *) userName Showwaininginview: (UIView *) view; // Verify user name input must be phone number + (BOOL) Checktelphone: (NSString *) Telphone Showwaininginview: (UIView *) view; // Verify that the user name is a normal account + (BOOL) Isusername: (NSString *) userName; // Verify that the user name is phone number + (BOOL) Istelphone: (NSString *) Telphone; @end

. m file

#import "nsstring+check.h"@implementationnsstring (Check)//You must start with a letter when verifying user name input+ (BOOL) Checkusername: (NSString *) userName Showwaininginview: (UIView *) view{//decide whether to start with a letterNSString *regex =@"^[a-za-z][a-za-z0-9]*$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:username]; if(!IsMatch) {[Mbprogresshud showerror:@"user name must start with a letter"Toview:view]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Mbprogresshud hidehudforview:view];        }); returnNO; }    returnYES;}//Verify that the user name is entered with a phone number+ (BOOL) Checktelphone: (NSString *) Telphone Showwaininginview: (UIView *) view{NSString*regex =@"^[1][358][0-9]{9}$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:telphone]; if(!IsMatch) {[Mbprogresshud showerror:@"cell phone number is not formatted correctly"Toview:view]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Mbprogresshud hidehudforview:view];        }); returnNO; }    returnYES;}//Verify that the user name is a normal account+ (BOOL) Isusername: (NSString *) username{NSString*regex =@"^[a-za-z][a-za-z0-9]*$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:username]; if(!IsMatch) {              returnNO; }    returnYES;}//Verify that the user name is a phone number+ (BOOL) Istelphone: (NSString *) telphone{NSString*regex =@"^[1][358][0-9]{9}$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:telphone]; if(!IsMatch) {        returnNO; }    returnYES;}@end

IOS: A regular expression that determines whether a user name starts with a letter and the phone number is entered correctly

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.