1. 程式人生 > >iOS正則表示式之區分大小寫中英文,判斷輸入框型別

iOS正則表示式之區分大小寫中英文,判斷輸入框型別

在開發過程中,有時需要對使用者輸入的型別做判斷,最常見是在註冊頁面即使用者名稱和密碼,直接上程式碼

#pragma - mark 只能為中文

-(BOOL)onlyInputChineseCharacters:(NSString*)string{

NSString *inputString =@"[\u4e00-\u9fa5]+";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",inputString];

BOOL inputString = [predicateevaluateWithObject

:string];

returninputString;

}

#pragma  - mark 只能為數字

- (BOOL)onlyInputTheNumber:(NSString*)string{

NSString *numString =@"[0-9]*";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",numString];

BOOL inputString = [predicateevaluateWithObject:string];

returninputString;

}

#pragma  - mark 只能為小寫

- (BOOL)onlyInputLowercaseLetter:(NSString*)string{

NSString *regex =@"[a-z]*";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL inputString = [predicateevaluateWithObject:string];

returninputString;

}

#pragma  - mark 只能為大寫

- (BOOL)onlyInputACapital:(NSString

*)string{

NSString *regex =@"[A-Z]*";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL inputString = [predicateevaluateWithObject:string];

returninputString;

}

#pragma  - mark 允許大小寫

- (BOOL)InputCapitalAndLowercaseLetter:(NSString*)string{

NSString *regex =@"[a-zA-Z]*";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL  inputString = [predicateevaluateWithObject:string];

return inputString;

}

#pragma - mark 允許含大小寫或數字(不限字數)

- (BOOL)inputLettersOrNumbers:(NSString*)string{

NSString *regex =@"[a-zA-Z0-9]*";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL  inputString = [predicateevaluateWithObject:string];

return inputString;

}

#pragma - mark 允許含大小寫或數字(限字數)

-(BOOL)inputNumberOrLetters:(NSString*)name {

NSString *userNameRegex =@"^[A-Za-z0-9]{6,20}+$";

NSPredicate *userNamePredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",userNameRegex];

BOOL  inputString = [userNamePredicateevaluateWithObject:name];

return inputString;

}

#pragma - mark 允許漢字或數字(不限字數)

- (BOOL)inputChineseOrNumbers:(NSString*)string{

NSString *regex =@"[\u4e00-\u9fa5]+[0-9]*";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL  inputString = [predicateevaluateWithObject:string];

return inputString;

}

#pragma - mark 允許漢字或數字(限字數)

- (BOOL)inputChineseOrNumbersLimit:(NSString*)string{

NSString *regex =@"[\u4e00-\u9fa5][0-9]{6,20}+$";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL  inputString = [predicateevaluateWithObject:string];

return inputString;

}

#pragma - mark 允許漢字,大小寫或數字(不限字數)

- (BOOL)inputChineseOrLettersAndNumbersNum:(NSString*)string{

NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]*";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL  inputString = [predicateevaluateWithObject:string];

return inputString;

}

#pragma - mark 允許漢字,大小寫或數字(限字數)

- (BOOL)inputChineseOrLettersNumberslimit:(NSString*)string{

NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]{6,20}+$";

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

BOOL  inputString = [predicateevaluateWithObject:string];

return inputString;

}