views:

182

answers:

1

Hi how can I validate email address, username, fullname and date of birth for my registration form inside an iphone application.

+4  A: 

You can use NSPredicate with regular expressions in iPhone OS > 3.0 like so

    - (BOOL) validateEmail: (NSString *) candidate {
       NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
       NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 

       return [emailTest evaluateWithObject:candidate];
    }
Submerged
thanks for the reply. Is there a way we can test phone numbers as well. I have a field which takes phone number as an input and I need to know whether he provided correct phone number. something like ping a phone number functionality.
Ayaz Alavi
To test phone numbers, the general rule is if it follows a certain format, it's valid. For security reasons, I am fairly certain that you can't "ping a phone". The only way to test it would be to have a database of customer phone records, but again...not something publicly available
Submerged