Home  >  Article  >  Web Front-end  >  How to determine whether to input in ios using regular expressions

How to determine whether to input in ios using regular expressions

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 10:46:241145browse

This time I will show you how to use regular expressions to determine whether to input in ios, and how to determine whether to input in regular expressions in ios.

can only be Chinese

-(BOOL)onlyInputChineseCharacters:(NSString*)string{
 NSString *zhString = @"[\u4e00-\u9fa5]+";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",zhString];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

can only be numbers

- (BOOL)onlyInputTheNumber:(NSString*)string{
 NSString *numString =@"[0-9]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numString];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

can only be lowercase

- (BOOL)onlyInputLowercaseLetter:(NSString*)string{
 NSString *regex =@"[a-z]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

*Only uppercase letters

- (BOOL)onlyInputACapital:(NSString*)string{
 NSString *regex =@"[A-Z]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

Allow uppercase and lowercase letters

- (BOOL)InputCapitalAndLowercaseLetter:(NSString*)string{
 NSString *regex =@"[a-zA-Z]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

Allow uppercase and lowercase letters or numbers (No limit on the number of characters)

- (BOOL)inputLettersOrNumbers:(NSString*)string{
 NSString *regex =@"[a-zA-Z0-9]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

Allows uppercase and lowercase letters or numbers (limit on the number of characters)

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

Allows Chinese characters or numbers (no limit on the number of characters)

- (BOOL)inputChineseOrNumbers:(NSString*)string{
 NSString *regex =@"[\u4e00-\u9fa5]+[0-9]*";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
 BOOL inputString = [predicate evaluateWithObject:string];
 return inputString;
}

Allows Chinese characters or numbers (limited number of characters)

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

Allows Chinese characters, uppercase and lowercase or numbers (limited number of characters)

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

Allows Chinese characters, uppercase and lowercase or numbers (limited number of characters)

- (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;
}
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!

Recommended reading:

How to write a regular expression to match a group of characters


Detailed explanation of positional matching of regular expressions

The above is the detailed content of How to determine whether to input in ios using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn