這次帶給大家正規怎麼在ios裡判斷是否輸入,正規在ios裡判斷是否輸入的注意事項有哪些,下面就是實戰案例,一起來看一下。
只能為中文
-(BOOL)onlyInputChineseCharacters:(NSString*)string{ NSString *zhString = @"[\u4e00-\u9fa5]+"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",zhString]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
只能為數字
- (BOOL)onlyInputTheNumber:(NSString*)string{ NSString *numString =@"[0-9]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numString]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
只能為小寫
- (BOOL)onlyInputLowercaseLetter:(NSString*)string{ NSString *regex =@"[a-z]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
*只能為大寫
- (BOOL)onlyInputACapital:(NSString*)string{ NSString *regex =@"[A-Z]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
#允許大小寫
- (BOOL)InputCapitalAndLowercaseLetter:(NSString*)string{ NSString *regex =@"[a-zA-Z]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
允許含大小寫或數字(不限字數)
- (BOOL)inputLettersOrNumbers:(NSString*)string{ NSString *regex =@"[a-zA-Z0-9]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
允許含大小寫或數字(限字數)
-(BOOL)inputNumberOrLetters:(NSString*)name { NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$"; NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex]; BOOL inputString = [userNamePredicate evaluateWithObject:name]; return inputString; }
允許漢字或數字(不限字數)
- (BOOL)inputChineseOrNumbers:(NSString*)string{ NSString *regex =@"[\u4e00-\u9fa5]+[0-9]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
允許漢字或數字(限字數)
- (BOOL)inputChineseOrNumbersLimit:(NSString*)string{ NSString *regex =@"[\u4e00-\u9fa5][0-9]{6,20}+$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
允許漢字,大小寫或數字(不限字數)
#- (BOOL)inputChineseOrLettersAndNumbersNum:(NSString*)string{ NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
允許漢字,大小寫或數字(限字數)
- (BOOL)inputChineseOrLettersNumberslimit:(NSString*)string{ NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]{6,20}+$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString; }
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
########################################################## ######以上是正規怎樣在ios裡判斷是否輸入的詳細內容。更多資訊請關注PHP中文網其他相關文章!