1.5 账号与安全
1.密码格式的限制
//判断密码含有数字以及字母
+(int)checkIsHaveNumAndLetter:(NSString*)password{
//数字条件
NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
//符合数字条件的有几个字节
NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:password
options:NSMatchingReportProgress
range:NSMakeRange(0, password.length)];
//英文字条件
NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
//符合英文字条件的有几个字节
NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];
//3,4 是满足条件的
if (tNumMatchCount == password.length) {
//全部符合数字,表示沒有英文
return 1;
} else if (tLetterMatchCount == password.length) {
//全部符合英文,表示沒有数字
return 2;
} else if (tNumMatchCount + tLetterMatchCount == password.length) {
//符合英文和符合数字条件的相加等于密码长度
return 3;
} else {
if (tNumMatchCount>0&&tLetterMatchCount>0) {
//包含英文和数字也包含标点
return 4;
}
//包含标点但可能不包含数字或者英文
return 5;
}
}
//判断密码包不包含空字符
+(BOOL) isBlankString:(NSString *)string {
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
};
//判断登录密码的长度
+(BOOL)secretLength:(NSString*)str{
if (str.length>7&&str.length<21) {
return YES;
}
return NO;
}
//清除两边的空字符
+(NSString *)clearWhiteKey:(NSString *)text{
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
text = [text stringByReplacingOccurrencesOfString:@"\r" withString:@""];
text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
return text;
};
//URL 编码 尝试转换成unnicode码没有成功,但是转化为urlencode后是可以的,服务器是http主站服务器
+ (NSString *)encodeToPercentEscapeString: (NSString *) input
{
// Encode all the reserved characters, per RFC 3986
// (<http: www.ietf.org="" rfc="" rfc3986.txt="">)
NSString *outputStr = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)input,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return outputStr;
}
//utf8转换成unicode编码 此处是由于服务器处理特殊字符有误导致我这边的utf8格式的编码到服务器后不能够被识别,在客户端转码之后正常,另外由于安卓机的特殊性,因此有些字符是需要被屏蔽掉的,也就是禁止用户输入,我使用的是发送通知
UITextFieldTextDidChangeNotification 并处理range 的location
+(NSString *) utf8ToUnicode:(NSString *)string
{
NSUInteger length = [string length];
NSMutableString *s = [NSMutableString stringWithCapacity:0];
for (int i = 0;i < length; i++)
{
unichar _char = [string characterAtIndex:i];
//判断是否为英文和数字
if (_char <= '9' && _char >='0')
{
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
}
else if(_char >='a' && _char <= 'z')
{
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
}
else if(_char >='A' && _char <= 'Z')
{
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
}
else
{
[s appendFormat:@"\\u%x",[string characterAtIndex:i]];
}
}
return s;
}
//顺便附上unicode转utf8代码
- (NSString *)replaceUnicode:(NSString *)unicodeStr
{
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
NSLog(@"%@",returnStr);
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}