1、数字字符校验
#define NUMBERSPERIOD @"0123456789."
- -(BOOL)checkNumber:(NSString *)string
- {
- NSCharacterSet*cs;
- cs =[[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD]invertedSet];
- NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
- BOOLbasicTest = [string isEqualToString:filtered];
- return basicTest;
- }
2、判断用户名,在2-16位
- -(BOOL)checkName:(NSString *)_text
- {
- NSString *Regex = @"^\\w{2,16}{1}quot;;
- NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
- return [Test evaluateWithObject:_text];
- }
3、判断手机号码,1开头的十一位数字
- -(BOOL)checkMobile:(NSString *)_text
- {
- NSString *Regex = @"1\\d{10}";
- NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
- return [Test evaluateWithObject:_text];
- }
4、判断邮箱
- -(BOOL)checkEmail:(NSString *)_text
- {
- NSString *Regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
- NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
- return [Test evaluateWithObject:_text];
- }
5、判断密码,6-16位
- -(BOOL)checkPassWord:(NSString *)_text
- {
- NSString *Regex = @"\\w{6,16}";
- NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
- return [Test evaluateWithObject:_text];
- }
只能输入非零开头的正整数用下面的代码
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *) string |
2 | { |
3 | NSString *result = [textField.text stringByReplacingCharactersInRange:range withString: string ]; |
4 | if ([result length] == 0) return YES; // Allow delete all character which are entered. |
5 | |
6 | NSString *regex = @"^[0-9]*[1-9][0-9]*$" ; |
7 | NSPredicate *prd = [NSPredicate predicateWithFormat: @"SELF MATCHES %@" , regex]; |
8 | return [prd evaluateWithObject:result]; |
9 | } |