Share the code UITextField (Validation) with a special restriction in the iOS input box)

Source: Internet
Author: User

Share the code UITextField (Validation) with a special restriction in the iOS input box)

// Personal summary. You are welcome to add or modify
# Import
 
  
Typedef enum {VALIDATION_TYPE_NUM_VALIDATED = 0, // number verification = 1, // ID card verification = 2, // mobile phone number verification = 3, // email} ValidationType; @ interface UITextField (Validation) -(void) setValidationType :( ValidationType) validationType;-(NSString *) errorMessage;-(void) limitTextLength :( int) length; @ end
 



# Import "UITextField + Validation. h" # import
 
  
# Import
  
   
Static NSString * identifier = @ "identifier"; static NSString * kLimitTextErrorMessageKey = @ "kLimitTextErrorMessageKey"; @ implementation UITextField (Validation)-(void) resetTextfieldValidation {identifier (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 = UIKeyboardTyp EEmailAddress;} 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 *) (kLimitTextErrorMessageKey); if (Str) {return str;} return nil;} # pragma mark-Limit Text Length-(void) limitTextLength :( int) length {objc_setAssociatedObject (self, (_ bridge const void *) (kLimitTextMaxLengthKey), [NSNumber numberWithInt: length], numeric); [self addTarget: self action: @ selector (operator :) forControlEvents: UIControlEventEditingChanged];} -(void) textFieldTextLengthLimit :( Id) sender {NSNumber * maxLengthNumber = objc_getAssociatedObject (self, (_ bridge const void *) (kLimitTextMaxLengthKey); int maxLength = [maxLengthNumber intValue]; if (self. text. length> maxLength) {self. text = [self. text substringToIndex: maxLength] ;}# pragma mark-Limit Text Only Number-(void) limitTextOnlyNumber {[self addTarget: self action: @ selector (textFieldTextNumberLimit :) forControlEvents: U IControlEventEditingChanged];}-(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: s Elf action: @ selector (textFieldTextPhoneLimit :) forControlEvents: limit]; [self limitTextLength: 11]; [self limitTextOnlyNumber];}-(void) handle :( id) sender {if (! Self. text. length) {[self resetTextfieldValidation]; return;} // NSString * regex = @ "^ (13 [0-9]) | (147) | (15 [^ 4, \ D]) | (18 [0, 5-9]) \ d {8} $ "; NSString * regex = @ "^ 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 resetTextfieldValida Tion];} else {self. text = [self. text substringToIndex: self. text. length]; objc_setAssociatedObject (self, (_ bridge const void *) (kLimitTextErrorMessageKey), @ "enter the correct phone number", OBJC_ASSOCIATION_RETAIN_NONATOMIC );}} # pragma mark-Limit Text For Email-(void) limitTextOnlyEmail {[self addTarget: self action: @ selector (textFieldTextForEmailLimit :) forControlEvents: UIControlEventEditingDidEnd];}-(void) textField TextForEmailLimit :( id) sender {if (! Self. text. length) {[self resetTextfieldValidation]; return;} NSString * regex = @ "^ [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 encoding: self. text] = YES) {[self resetTextfieldValidation];} else {objc_setAssociatedObject (self, (_ Bridge const void *) (kLimitTextErrorMessageKey), @ "Mailbox format error", OBJC_ASSOCIATION_RETAIN_NONATOMIC) ;}# pragma mark-Limit Text For IDCard-(void) limitTextOnlyIDCard {[self addTarget: self action: @ selector (role :) forControlEvents: role]; [self limitTextLength: 18];}-(void) textFieldTextForIDCardLimit :( id) sender {if (! Self. text. length) {[self resetTextfieldValidation]; return;} // NSString * regex = @ "^ (4 \ d {12 }(?: \ D {3 })?) $ "; NSString * regex = @" ^ ([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), @ "Incorrect ID card format ", parameters) ;}# pragma mark-Limit Text NoSpace-(void) limitTextNoSpace {[self addTarget: self action: @ selector (textFieldTextNoSpaceLimit :) forControlEvents: parameters];}-(void) textFieldTextNoSpaceLimit :( id) sender {self. text = [self noSpaceString: self. text];}-(NSString *) noSpaceString :( NSString *) str {if (str. length) {return [str stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet];} return str ;}@ end
  
 


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.