Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

IOS regular expression determines the type of input

Shulou Source: shulou.com Published: 2022-06-02 06:38:15 10月29日 Update

This article will explain in detail about the IOS regular expression to determine the input type, the editor thinks it is very practical, so share it with you as a reference, I hope you can get something after reading this article.

In the process of development, it is sometimes necessary to judge the type entered by the user. The most common is the user name and password on the registration page. The code is sorted out as follows:

Only for 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 Murz] *"; NSPredicate * predicate = [NSPredicate predicateWithFormat:@ "SELF MATCHES% @", regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString;}

* uppercase only

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

Allow case

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

Uppercase or numeric allowed (unlimited number of words)

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

Uppercase and lowercase or numbers are allowed (word limit)

-(BOOL) inputNumberOrLetters: (NSString*) name {NSString* userNameRegex = @ "^ [A-Za-z0-9] {6 NSPredicate predicateWithFormat:@ 20} + $"; NSPredicate * userNamePredicate = [NSPredicate predicateWithFormat:@ "SELF MATCHES% @", userNameRegex]; BOOL inputString = [userNamePredicate evaluateWithObject:name]; return inputString;}

Allow Chinese characters or numbers (unlimited 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;}

Allow Chinese characters or numbers (limit the number of characters)

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

Allow Chinese characters, upper and lower case or numbers (unlimited number of words)

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

Allow Chinese characters, uppercase and lowercase or numbers (limited to the number of characters)

-(BOOL) inputChineseOrLettersNumberslimit: (NSString*) string {NSString* regex = @ "[\ u4e00 -\ u9fa5] + [A-Za-z0-9] {6 predicate 20} + $"; NSPredicate * predicate = [NSPredicate predicateWithFormat:@ "SELF MATCHES% @", regex]; BOOL inputString = [predicate evaluateWithObject:string]; return inputString } this is the end of the article on "judging input types by IOS regular expressions". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Number word count size Chinese character type input article regular expression more user good practical code content uppercase password lowercase common article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Apple MySQL vpn MariaDB