2016-07-27 9 views
0

Ich verwende einige Textfelder und Textfeldvalidierungen. Ich habe unten meinen Code gezeigt, in diesem Code klicken Sie Knopfereignis und den gesamten Textfeldtext, der auf Webserver, in dieser Codeüberprüfung gespeichert wird, die nur auf leerem Textfeld läuft. Aber ich möchte E-Mail-Adresse Validierung und alle Textfeld Zeichenlänge behoben.Ich habe versucht, viele Male aber einige Zeit Zustand falsch und einige Zeit zeigen keine Alarmansicht und einige Zeit nicht speichern Textfeld Text. Wie es möglich, bitte helfen, DankeSo legen Sie die Validierung in UITextFields fest

Mein Code

- (IBAction)submit:(id)sender { 

if(self.txname == nil || [self.txname.text isEqualToString:@""]) 
{ 
    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Name"message:@"All Fields are mandatory." delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil]; 
    [ErrorAlert show]; 

} 

else if(self.txemail == nil || [self.txemail.text isEqualToString:@""]) 
{ 

    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Email"message:@"All Fields are mandatory." delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil]; 
    [ErrorAlert show]; 


} 

else if(self.tx_phone == nil || [self.tx_phone.text isEqualToString:@""]) 
{ 
    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Phone"message:@"All Fields are mandatory." delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil]; 
    [ErrorAlert show]; 


} 
else if(self.txcomment == nil || [self.txcomment.text isEqualToString:@""]) 
{ 
    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Comment"message:@"All Fields are mandatory." delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil]; 
    [ErrorAlert show]; 


} 


else 
{ 


//Here YOUR URL 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY URL"]]; 


//create the Method "GET" or "POST" 
[request setHTTPMethod:@"POST"]; 

//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS) 
NSString *userUpdate =[NSString stringWithFormat:@"name=%@&email=%@&phone=%@& comment=%@&",_txname.text,_txemail.text,_tx_phone.text,_txcomment.text,nil]; 



//Check The Value what we passed 
NSLog(@"the data Details is =%@", userUpdate); 

//Convert the String to Data 
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; 

//Apply the data to the body 
[request setHTTPBody:data1]; 

//Create the response and Error 
NSError *err; 
NSURLResponse *response; 

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding]; 

//This is for Response 
NSLog(@"got response==%@", resSrt); 
if(resSrt) 
{ 
    NSLog(@"got response"); 

} 
else 
{ 
    NSLog(@"faield to connect"); 
} 
    { 
     UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Success"message:@"All Fields are mandatory." delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil]; 
     [ErrorAlert show]; 


    } 
    [self.view endEditing:YES]; 
    [email protected]""; 
    [email protected]""; 
    [email protected]""; 
    [email protected]""; 


} 
} 

Antwort

1

Ich habe es in meinem Code, Sie wie folgt:

- (IBAction)submit:(id)sender { 

     if (![self isFormValid]) { 

      return; 

     } 

    NSError *error; 


    if (!error) 
    { 
     UIAlertView *signupalert = [[UIAlertView alloc]initWithTitle:@"Congratulations" message:@"Record Added Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

     [signupalert show]; 

    } 

} 

-(BOOL)isFormValid 
{ 

    NSString *emailRegEx [email protected]"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 

    NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegEx]; 



    if (txname.text && txname.text.length==0) 
    { 
     [self showErrorMessage:@"Please enter name"]; 
     return NO; 
    } 

    else if (tx_phone.text && tx_phone.text.length!=10) 
    { 
     [self showErrorMessage:@"Please enter valid phone number"]; 
     return NO; 
    } 

    else if([emailTest evaluateWithObject: txemail.text]==NO) 
    { 
     [self showErrorMessage:@"Please enter Valid Email_id"]; 
     return NO; 
    } 
    else if (txcomment.text && txcomment.text.length==0) 
    { 
     [self showErrorMessage:@"Please enter comment"]; 
     return NO; 
    } 

    return YES; 
} 

-(void)showErrorMessage:(NSString *)message 
{ 

     UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

     [alertmessage show]; 

    } 
+0

Undeclared Bezeichner: - isFormValid –

+0

@Ankur Aktualisierte kleine Änderungen in meinem Code, bitte überprüfen Sie es einmal .., die Ihr Problem lösen .. :) –

+0

Es funktioniert. danke, aber Name Textfeld Länge wie zu setzen –

0
-(BOOL) NSStringIsValidEmail:(NSString *)checkEmail 
{ 
    BOOL stricterFilter = NO; 
    NSString *filter = @"^[A-Z0-9a-z\\._%+-][email protected]([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$"; 
     NSString *lstring = @"^[email protected]([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$"; 
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
return [emailTest evaluateWithObject:checkEmail]; 
} 
+0

wo u diese Methoden –

+0

implementieren Wie in verwenden, Mein Code –

2

tun wie

- (IBAction)submit:(id)sender { 

    if (![txname hasText]) { 
    [self showAlertView:@"Alert" message:@"name is empty"]; 
    } 
    else if (![txemail hasText]) 
    { 
    [self showAlertView:@"Alert" message:@"email is empty"]; 
    } 
    else if ([self isValidEmailAddress:txemail.text] == NO) 
    { 
    [self showAlertView:@"Alert" message:@"Invaildemail"]; 
    } 
    else 
    { 
    // call webservice for succes 
    } 

Erstellen Sie die alertcontroller

- (void)showAlertView:(NSString*)title message:(NSString*)message 
{ 
UIAlertController* alertMessage = [UIAlertController 
    alertControllerWithTitle:title 
        message:message 
       preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction* yesButton = [UIAlertAction 
    actionWithTitle:@"OK" 
       style:UIAlertActionStyleDefault 
      handler:^(UIAlertAction* action){ 
      }]; 

[alertMessage addAction:yesButton]; 

[self presentViewController:alertMessage animated:YES completion:nil]; 
} 

für E-Mail-Validierung

- (BOOL)isValidEmailAddress:(NSString *)emailAddress 
{ 
//Create a regex string 
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" ; 

//Create predicate with format matching your regex string 
NSPredicate *emailTest = [NSPredicatepredicateWithFormat: 
          @"SELF MATCHES %@", stricterFilterString]; 

//return true if email address is valid 
return [emailTest evaluateWithObject:emailAddress]; 
} 

aktualisiert

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
if (textField == self.txname) 
{ 
// Prevent crashing undo bug – see note below. 
if(range.length + range.location > textField.text.length) 
{ 
    return NO; 
} 

NSUInteger newLength = [textField.text length] + [string length] - range.length; 
return newLength <= 25; 
} 
return YES; 
} 
+0

@AnkurKumawat - überprüfen Sie die aktualisierte answew –

+0

@ AnkurKumawat - der Grund, warum ich 'Dism iss 'als Makros, aber ich habe hier als 'OK' geändert, überprüfen Sie die aktualisierte Antwort –

+0

@AnkurKumawat - Zeichenlänge ..? wo –

Verwandte Themen