2011-10-05 17 views
7

Ho bisogno di un modo per garantire che i numeri di telefono abbiano 10 cifre senza altri caratteri, ad esempio() - e assicurarsi che gli indirizzi di posta elettronica siano e-mail valide (formattate correttamente).Validazione campo di testo iOS

C'è qualche libreria che non può renderlo facile per me quindi non devo scrivere espressioni regolari.

risposta

17

Verificherà un UITextField per un'e-mail e un numero di telefono corretti di 10 cifre o meno.
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 campo di testo, quanti caratteri sono attualmente in essa, e quali caratteri si vuole aggiungere:

#define ALPHA     @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 
#define NUMERIC     @"1234567890" 
#define ALPHA_NUMERIC   ALPHA NUMERIC 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    NSCharacterSet *unacceptedInput = nil; 
    switch (textField.tag) { 
     // Assuming EMAIL_TextField.tag == 1001 
     case 1001: 
      if ([[textField.text componentsSeparatedByString:@"@"] count] > 1) 
       unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet]; 
      else 
       unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".!#$%&'*+-/=?^_`{|}[email protected]"]] invertedSet]; 
      break; 
     // Assuming PHONE_textField.tag == 1002 
     case 1002: 
      if (textField.text.length + string.length > 10) { 
       return NO; 
      } 
      unacceptedInput = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; 
      break; 
     default: 
      unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet]; 
      break; 
    } 
    return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1); 
} 

Inoltre, prova anche questi 2 articoli:
Auto-formatting phone number UITextField on the iPhone
PhoneNumberFormatter.

+0

sembra buono, non ho guardato nella delegato campo di testo prima. – sunkencity

+0

chown, funziona alla grande ma non sembra gestire gli eventi chiave di cancellazione. Restituisce NO se il telefono è> = 10 quindi non cambia dopo l'eliminazione – spentak

+0

Oh, sei corretto. Sostituisci 'if (textField.text.length> = 10)' con 'if (textField.text.length + string.length> 10)' – chown

3

Ecco un modo semplice per garantire la lunghezza del phonenumber in UIViewController che ha il campo di testo nella sua vista.

- (void)valueChanged:(id)sender 
{ 
    if ([[[self phoneNumberField] text] length] > 10) { 
     [[self phoneNumberField] setText:[[[self phoneNumberField] text] 
      substringToIndex:10]]; 
    } 
} 

- (void) viewWillAppear:(BOOL)animated 
{ 
    [[self phoneNumberField] addTarget:self 
          action:@selector(valueChanged:) 
          forControlEvents:UIControlEventEditingChanged]; 
} 

Per e-mail, si supponga di voler controllare una regexp quando perde lo stato attivo.

0

Ecco semplice esempio per UITextField convalida mentre il tipo di tastiera in altri personaggi che non visualizza

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
//UITextField *tf_phonenumber,*tf_userid; 
if (textField.text<=10) { 
char c=*[string UTF8String]; 
if (tf_phonenumber==textField)  //PhoneNumber /Mobile Number 
{ 
    if ((c>='0' && c<='9')||(c==nil)) { 
     return YES; 
    } 
    else 
     return NO; 
} 
if (tf_userid==textField)   //UserID validation 
{ 
    if ((c>='a' && c<='z')||(c>='A' && c<='Z')||(c==' ')||(c==nil)) { 
     return YES; 
    } 
    else 
     return NO; 
} 
    return YES; 
} 
else{ 
    return NO; 
} 
} 
Problemi correlati