gpt4 book ai didi

ios - 如何在 UITextFields 中设置验证

转载 作者:行者123 更新时间:2023-11-28 19:36:12 24 4
gpt4 key购买 nike

我正在使用一些文本字段和文本字段验证。我在下面显示了我的代码,在此代码中,单击按钮事件和所有文本字段文本都保存在 Web 服务器上,在此代码验证中,仅在空文本字段上运行。但我想更正电子邮件地址验证和固定所有文本字段字符长度。我尝试了很多次,但有时条件错误,有时不显示警报 View ,有时不保存文本字段文本。怎么可能请帮忙,谢谢

我的代码

- (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];
self.txname.text=@"";
self.txemail.text=@"";
self.tx_phone.text=@"";
self.txcomment.text=@"";


}
}

最佳答案

喜欢

- (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
}

创建警报 Controller

- (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];
}

用于电子邮件验证

-  (BOOL)isValidEmailAddress:(NSString *)emailAddress
{
//Create a regex string
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[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];
}

已更新

- (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;
}

关于ios - 如何在 UITextFields 中设置验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38608133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com