我正在使用一些文本字段和文本字段驗證。我在下面顯示了我的代碼,在此代碼中單擊按鈕事件和所有文本字段文本保存在Web服務器上,在此代碼驗證中僅在空白文本字段中運行。但我想糾正電子郵件地址驗證和所有文本字段字符長度固定。我試了很多次,但一些時間條件錯誤,有些時候不顯示警報視圖,有些時候不保存文本字段文本。它是如何可能的話請幫忙,謝謝如何在UITextFields中設置驗證
我的代碼
- (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]"";
}
}
未聲明的標識符: - isFormValid –
@Ankur更新了我的代碼中的小改動,請檢查一次..解決你的問題.. :) –
它的工作。謝謝,但名稱文本字段字符長度如何設置 –