欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

IOS正则表达式判断输入类型(整理)

程序员文章站 2022-03-10 21:55:32
在开发过程中,有时需要对用户输入的类型做判断,最常见是在注册页面即用户名和密码,代码整理如下: 只能为中文 -(bool)onlyinputchinesecha...

在开发过程中,有时需要对用户输入的类型做判断,最常见是在注册页面即用户名和密码,代码整理如下:

只能为中文

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

以上所述是小编给大家介绍的ios正则表达式判断输入类型(整理),希望对大家有所帮助