码迷,mamicode.com
首页 > 移动开发 > 详细

分享一个iOS输入框特殊限制的代码 UITextField (Validation)

时间:2014-08-25 22:52:46      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:ios开发

//个人总结,欢迎新增或修改
#import <UIKit/UIKit.h>
typedef enum{
    VALIDATION_TYPE_NUM_VALIDATED = 0,//数字
    VALIDATION_TYPE_ID_CARD_VALIDATED = 1,//身份证
    VALIDATION_TYPE_MOBILE_PHONE_VALIDATED = 2,//手机号
    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 *)(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], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self addTarget:self action:@selector(textFieldTextLengthLimit:) 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: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(textFieldTextPhoneLimit:) forControlEvents:UIControlEventEditingDidEnd];
    [self limitTextLength:11];
    [self limitTextOnlyNumber];
}

- (void)textFieldTextPhoneLimit:(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 resetTextfieldValidation];
    }else{
        self.text=[self.text substringToIndex:self.text.length];
        objc_setAssociatedObject(self, (__bridge  const void *)(kLimitTextErrorMessageKey), @"请输入正确的手机号码", OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}
#pragma mark - Limit Text For Email
-(void)limitTextOnlyEmail
{
    [self addTarget:self action:@selector(textFieldTextForEmailLimit:) forControlEvents:UIControlEventEditingDidEnd];
}

- (void)textFieldTextForEmailLimit:(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 evaluateWithObject:self.text]==YES) {
        [self resetTextfieldValidation];
    }else{
        objc_setAssociatedObject(self, (__bridge  const void *)(kLimitTextErrorMessageKey), @"邮箱格式错误", OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}
#pragma mark - Limit Text For IDCard
-(void)limitTextOnlyIDCard
{
    [self addTarget:self action:@selector(textFieldTextForIDCardLimit:) forControlEvents:UIControlEventEditingDidEnd];
    [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), @"身份证格式错误", OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

#pragma mark - Limit Text NoSpace
- (void)limitTextNoSpace
{
    [self addTarget:self action:@selector(textFieldTextNoSpaceLimit:) forControlEvents:UIControlEventEditingDidEnd];
}

- (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


分享一个iOS输入框特殊限制的代码 UITextField (Validation)

标签:ios开发

原文地址:http://blog.csdn.net/iitvip/article/details/38828443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!