Verificherà un UITextField per un'e-mail corretta.
Aggiungere questo metodo allo textFields delegate quindi verificare se i caratteri che sta per cambiare devono essere aggiunti o meno.
ritorno YES
o NO
a seconda del testo attuale campi di testo rispetto ad un indirizzo email valido:
#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define NUMERIC @"1234567890"
#define ALPHA_NUMERIC ALPHA NUMERIC
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *unacceptedInput = nil;
if ([[textField.text componentsSeparatedByString:@"@"] count] > 1) {
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet];
} else {
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".!#$%&'*+-/=?^_`{|}[email protected]"]] invertedSet];
}
return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);
}
Per verificare se un campo di testo è vuota o non basta usare if (myTextField.text.length > 0) {}
ovunque nel vostro controller della vista.
http://stackoverflow.com/a/3638271/644149 è buon modo –