在開發(fā)過程中,有時需要對用戶輸入的類型做判斷,最常見是在注冊頁面即用戶名和密碼,代碼整理如下:
只能為中文
-(BOOL)onlyInputChineseCharacters:(NSString*)string{
NSString *zhString = @"[\u4e00-\u9fa5]+";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",zhString];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
只能為數(shù)字
- (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;
}
允許含大小寫或數(shù)字(不限字數(shù))
- (BOOL)inputLettersOrNumbers:(NSString*)string{
NSString *regex =@"[a-zA-Z0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
允許含大小寫或數(shù)字(限字數(shù))
-(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;
}
允許漢字或數(shù)字(不限字數(shù))
- (BOOL)inputChineseOrNumbers:(NSString*)string{
NSString *regex =@"[\u4e00-\u9fa5]+[0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
允許漢字或數(shù)字(限字數(shù))
- (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;
}
允許漢字,大小寫或數(shù)字(不限字數(shù))
- (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;
}
允許漢字,大小寫或數(shù)字(限字數(shù))
- (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;
}
以上所述是小編給大家介紹的IOS正則表達式判斷輸入類型(整理),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- iOS中正則表達式的運用示例代碼
- IOS中用正則表達式判斷輸入的內容為8-16位且同時包含數(shù)字和字母
- iOS中常見正則表達式驗證方法
- 淺談iOS應用中的相關正則及驗證
- iOS 正則表達式判斷手機號碼、固話
- iOS 正則表達式判斷純數(shù)字及匹配11位手機號碼的方法
- iOS開發(fā)驗證判斷語句之正則表達式小結
- iOS開發(fā) 正則運算詳細介紹
- iOS 正則表達式詳解
- IOS開發(fā)常用的正則表達式
- iOS中使用正則表達式NSRegularExpression 來驗證textfiled輸入的內容
- IOS中NSPredicate和NSRegularExpression校驗正則表達式區(qū)別