Share an iOS input box special restrictions code Uitextfield (Validation)

Source: Internet
Author: User

Personal Summary, welcome to add or modify
#import <uikit/uikit.h>typedef enum{    validation_type_num_validated = 0,//Digital    validation_type_id_card_ VALIDATED = 1,//id    validation_type_mobile_phone_validated = 2,//phone number    validation_type_email_validated = 3,// email} validationtype; @interface Uitextfield (Validation)-(void) Setvalidationtype: (ValidationType) ValidationType ;-(NSString *) errormessage;-(void) limittextlength: (int) length; @end



#import "Uitextfield+validation.h" #import <objc/objc.h> #import <objc/runtime.h>static NSString * Klimittextmaxlengthkey = @ "Klimittextmaxlengthkey"; static nsstring *klimittexterrormessagekey = @ " Klimittexterrormessagekey "; @implementation Uitextfield (Validation)-(void) resettextfieldvalidation{objc_ Setassociatedobject (self, (__bridge const void *) (Klimittexterrormessagekey), nil, objc_association_retain_nonatomic );} -(void) Setvalidationtype: (ValidationType) validationtype{[Self addtarget:self Action: @selector (    resettextfieldvalidation) Forcontrolevents:uicontroleventeditingdidbegin];        Self.keyboardtype = Uikeyboardtypedefault;        if (ValidationType = = validation_type_num_validated) {[Self limittextonlynumber];    Self.keyboardtype = Uikeyboardtypenumberpad;        }else if (ValidationType = = validation_type_email_validated) {[Self limittextonlyemail];    Self.keyboardtype = uikeyboardtypeemailaddress; }else if (ValidationType = = Validation_type_mobile_phone_validated) {[Self limittextonlyphone];    Self.keyboardtype = Uikeyboardtypephonepad;    }else if (ValidationType = = validation_type_id_card_validated) {[Self limittextonlyidcard]; } [self limittextnospace];} -(NSString *) errormessage{nsstring *str= objc_getassociatedobject (self, (__bridge const void *) (KLIMITTEXTERRORMESSAG    EKey));    if (str) {return str; } return nil;} #pragma mark-limit Text length-(void) limittextlength: (int) length{objc_setassociatedobject (self, (__bridge const VO    ID *) (klimittextmaxlengthkey), [NSNumber numberwithint:length], objc_association_retain_nonatomic); [Self addtarget:self Action: @selector (Textfieldtextlengthlimit:) forcontrolevents:uicontroleventeditingchanged];} -(void) Textfieldtextlengthlimit: (ID) sender{NSNumber *maxlengthnumber = Objc_getassociatedobject (self, (__bridge con    St void *) (Klimittextmaxlengthkey));    int maxLength = [Maxlengthnumber intvalue]; if (Self.text.length &GT    MaxLength) {self.text = [self.text substringtoindex:maxlength]; }} #pragma mark-limit Text only number-(void) limittextonlynumber{[Self addtarget:self Action: @selector (Textfieldtextn Umberlimit:) forcontrolevents:uicontroleventeditingchanged];}        -(void) Textfieldtextnumberlimit: (ID) sender{if (!self.text.length) {[Self resettextfieldvalidation];    Return    } NSString * Regexnum = @ "^\\d*$";    Nspredicate *regexnumpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Regexnum]; if ([Regexnumpredicate evaluatewithobject:self.text]==yes) {}else{Self.text=[self.text substringFromIndex:self    . Text.length]; }} #pragma mark-limit Text only phone-(void) limittextonlyphone{[Self addtarget:self Action: @selector (Textfieldtextpho    Nelimit:) forcontrolevents:uicontroleventeditingdidend];    [Self limittextlength:11]; [Self limittextonlynumber];} -(void) Textfieldtextphonelimit: (ID) sender{if (!self.text.length) {[Self ReseTtextfieldvalidation];    Return }//NSString * [email protected] "^ ((13[0-9]) | ( 147) | (15[^4,\\d]) | (18[0,5-9]))    \\d{8}$ ";    NSString * [email protected] "^1\\d{10}$";    Nspredicate *regexpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];        if ([Regexpredicate evaluatewithobject:self.text]==yes) {self.text=[self.text substringToIndex:self.text.length];    [Self resettextfieldvalidation];        }else{Self.text=[self.text SubstringToIndex:self.text.length]; Objc_setassociatedobject (self, (__bridge const void *) (Klimittexterrormessagekey), @ "Please enter the correct mobile number", Objc_association_    Retain_nonatomic); }} #pragma mark-limit Text for email-(void) limittextonlyemail{[Self addtarget:self Action: @selector (Textfieldtextfore Maillimit:) forcontrolevents:uicontroleventeditingdidend];}        -(void) Textfieldtextforemaillimit: (ID) sender{if (!self.text.length) {[Self resettextfieldvalidation];    Return } nsstring *[emaiL protected] "^[a-za-z0-9][\\w\\.-]*[a-za-z0-9]@[a-za-z0-9][\\w\\.-]*[a-za-z0-9]\\. [A-za-z] [A-za-z\\.]    *[a-za-z]$ ";    Nspredicate *regexpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];    if ([Regexpredicate evaluatewithobject:self.text]==yes) {[Self resettextfieldvalidation]; }else{Objc_setassociatedobject (self, (__bridge const void *) (Klimittexterrormessagekey), @ "Mailbox format error", Objc_associat    Ion_retain_nonatomic); }} #pragma mark-limit Text for idcard-(void) limittextonlyidcard{[Self addtarget:self Action: @selector (TEXTFIELDTEXTFO    Ridcardlimit:) forcontrolevents:uicontroleventeditingdidend]; [Self limittextlength:18];}        -(void) Textfieldtextforidcardlimit: (ID) sender{if (!self.text.length) {[Self resettextfieldvalidation];    Return }//nsstring *[email protected] "^ (4\\d{12} (?: \ \d{3})?)    $"; NSString *[email protected] "^ ([1-9]\\d{7} ((0\\d) | ( 1[0-2]) (([0|1|2]\\d) |3[0-1]) \\d{3}) | ([1-9]\\d{5}[1-9]\\d{3} ((0\\d) | (1[0-2])) ([0|1|2]\\d) |3[0-1]) \\d{3} ([0-9]|    X)) $ ";    Nspredicate *regexpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];    if ([Regexpredicate evaluatewithobject:self.text]==yes) {[Self resettextfieldvalidation]; }else{Objc_setassociatedobject (self, (__bridge const void *) (Klimittexterrormessagekey), @ "ID format error", Objc_associa    Tion_retain_nonatomic); }} #pragma mark-limit Text nospace-(void) limittextnospace{[Self addtarget:self Action: @selector (Textfieldtextnospace Limit:) forcontrolevents:uicontroleventeditingdidend];} -(void) Textfieldtextnospacelimit: (id) sender{self.text = [self noSpaceString:self.text];} -(NSString *) nospacestring: (NSString *) str{if (str.length) {return [str stringbytrimmingcharactersinset: [NS]    CharacterSet Whitespacecharacterset]]; } return str;} @end


Share an iOS input box special restrictions code Uitextfield (Validation)

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.