1

我正在编写一个具有注册部分的 iOS 应用程序。我的客户有这些糟糕的验证规则,这让我抓狂。最新的规则是这样的:不要接受超过 3 个按字母顺序排列的字符,例如:“abcd”、“eFgH”、“jklM”。

但我可以按“1234”、“3456”之类的顺序接受数字...

为了解决这些问题,我已经在使用 NSPredicate 和 NSRegularExpression。但我不知道用正则表达式来识别这些字符,所以我请求你的帮助。

有谁知道如何解决这个问题?

4

2 回答 2

2

让我祝贺你他们还没有注意到键盘没有字母布局:)

NSString * str = [@"01234abcdsfsaasgAWEGFWAE" lowercaseString]; // make it a lower case string as you described it not case-sensitive
const char * strUTF8 = [str UTF8String]; // get char* password text for the numerical comparison

BOOL badPassword = NO;
int charIndex = 0;
int badHitCount = 0;
const int len = strlen(strUTF8);
char previousChar = strUTF8[0]; // the app is going to crash here with an empty string

// check the password
while (charIndex < len) {
    char currentChar = strUTF8[charIndex++];
    if (currentChar - previousChar == 1 && (currentChar >= 57 || currentChar <= 48)) 
    // 57 is the character '9' index at UTF8 table, letters are following this index, some characters are located before 48's '0' character though
        badHitCount++;
    else
        badHitCount = 0;
    previousChar = currentChar;

    if (badHitCount >= 3) {
        badPassword = YES;
        break;
    }
}

if (badPassword) {
    NSLog(@"You are a Bad User !");
} else {
    NSLog(@"You are a Good User !");
}
于 2012-10-29T17:17:50.057 回答
1

从可能可行的最简单的事情开始:

BOOL hasAlphabetSequence(NSString *s, int sequenceLength) {
    static NSString *const alphabet = @"abcdefghijklmnopqrstuvwxyz";
    s = [s lowercaseString];
    for (int i = 0, l = (int)alphabet.length - sequenceLength; i < l; ++i) {
        NSString *sequence = [alphabet substringWithRange:NSMakeRange(i, sequenceLength)];
        if ([s rangeOfString:sequence].location != NSNotFound) {
            return YES;
        }
    }
    return NO;
}
于 2012-10-29T17:44:22.957 回答