Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: how to check if a string has only digits?

I need to determine if a text is an email address, or mobile number, for email address I can use some regular expression, for mobile number I can check if the string has only digits(right?)

and the sequence is like:

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

but how do I check if a string has only numbers in iOS?

Thanks

like image 922
hzxu Avatar asked Sep 24 '12 02:09

hzxu


1 Answers

You create an NSCharacterSet that includes the digits and probably the dash and maybe parentheses (depending on what format you are seeing for the phone numbers). You then invert that set, so you have a set that has everything but those digits and things, then use rangeOfCharactersFromSet and if you get anything but NSNotFound, then you have something other than digits.

like image 108
rdelmar Avatar answered Sep 23 '22 07:09

rdelmar