5

テキストがメールアドレスなのか携帯電話番号なのかを判断する必要があります。メールアドレスの場合は正規表現を使用でき、携帯電話番号の場合は文字列に数字しかないかどうかを確認できます(右?)

シーケンスは次のようになります。

is (regex_valid_email(text))
{
    // email
}
else if (all_digits(text))
{
    // mobile number
}

しかし、iOSで文字列に数字しかないかどうかを確認するにはどうすればよいですか?

ありがとう

4

2 に答える 2

10

数字と、おそらくダッシュとかっこを含むNSCharacterSetを作成します(電話番号に表示されている形式によって異なります)。次に、そのセットを反転して、それらの数字以外のすべてを含むセットを作成し、rangeOfCharactersFromSetを使用します。NSNotFound以外のものを取得した場合は、数字以外のものを使用します。

于 2012-09-24T02:21:28.783 に答える
5

これは機能するはずです:

//This is the input string that is either an email or phone number
NSString *input = @"18003234322";

//This is the string that is going to be compared to the input string
NSString *testString = [NSString string];

NSScanner *scanner = [NSScanner scannerWithString:input];

//This is the character set containing all digits. It is used to filter the input string
NSCharacterSet *skips = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];

//This goes through the input string and puts all the 
//characters that are digits into the new string
[scanner scanCharactersFromSet:skips intoString:&testString];

//If the string containing all the numbers has the same length as the input...
if([input length] == [testString length]) {

    //...then the input contains only numbers and is a phone number, not an email
}
于 2012-09-24T03:11:40.140 に答える