IOS应用程序中的验证
我知道这是一个简单的问题,但我已经尝试了很多次代码检查验证,但都不起作用。当我点击注册按钮时,它会将我带到下一页,而不是验证我的表单。我的代码是IOS应用程序中的验证,ios,objective-c,Ios,Objective C,我知道这是一个简单的问题,但我已经尝试了很多次代码检查验证,但都不起作用。当我点击注册按钮时,它会将我带到下一页,而不是验证我的表单。我的代码是 - (IBAction)Register:(id)sender { if ([self.name.text isEqualToString:@""]){ NSString *message = @"Name"; UIAlertController *alert = [UIAlertController aler
- (IBAction)Register:(id)sender {
if ([self.name.text isEqualToString:@""]){
NSString *message = @"Name";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Your Name"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
} else if ([self.email.text isEqualToString:@""]){
NSString *message = @"Email";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Your Email Address"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}else{
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:_email.text] == YES)
{
// NSLog(@"Correct Email");
}
else
{
NSString *message = @"Invalid Email";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Email Format Incorrect"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([_code.titleLabel.text isEqualToString:@"Code"]) {
NSString *message = @"Select Country Code";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([self.mobileCode.text isEqualToString:@""]) {
NSString *message = @"Enter Mobile Code";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([self.number1.text isEqualToString:@""]) {
NSString *message = @"Enter Your Number";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
}
[self Account];
}
似乎您正在调用API,即使所有验证都已通过或失败。我把小逻辑放在何时调用API上。请检查我的下面,让我知道如果你有任何疑问
- (IBAction)Register:(id)sender {
BOOL isValidationSuccess = TRUE;
if ([self.name.text isEqualToString:@""]){
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Name";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Your Name"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
} else if ([self.email.text isEqualToString:@""]){
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Email";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Your Email Address"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}else{
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:_email.text] == YES)
{
// NSLog(@"Correct Email");
}
else
{
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Invalid Email";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Email Format Incorrect"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([_code.titleLabel.text isEqualToString:@"Code"]) {
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Select Country Code";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([self.mobileCode.text isEqualToString:@""]) {
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Enter Mobile Code";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([self.number1.text isEqualToString:@""]) {
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Enter Your Number";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
}
// This comes TRUE means you have pass your all validation.
if(isValidationSuccess)
{
/// You can call for register
[self account];
}
}
似乎您正在调用API,即使所有验证都已通过或失败。我把小逻辑放在何时调用API上。请检查我的下面,让我知道如果你有任何疑问
- (IBAction)Register:(id)sender {
BOOL isValidationSuccess = TRUE;
if ([self.name.text isEqualToString:@""]){
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Name";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Your Name"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
} else if ([self.email.text isEqualToString:@""]){
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Email";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Your Email Address"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}else{
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:_email.text] == YES)
{
// NSLog(@"Correct Email");
}
else
{
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Invalid Email";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Email Format Incorrect"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([_code.titleLabel.text isEqualToString:@"Code"]) {
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Select Country Code";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([self.mobileCode.text isEqualToString:@""]) {
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Enter Mobile Code";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
if ([self.number1.text isEqualToString:@""]) {
// You need to change the flage when you get error in your code
isValidationSuccess = FALSE;
NSString *message = @"Enter Your Number";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
int duration = 1; // duration in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
}
// This comes TRUE means you have pass your all validation.
if(isValidationSuccess)
{
/// You can call for register
[self account];
}
}
验证
textfield
的最佳方法是
创建一个验证方法
-(BOOL)validation
{
if (self.name.text.length==0){
// Show alert enter Name
return NO;
}
if ([self.email.text.length==0){
// Show alert enter Email
return NO;
}
if ([_code.titleLabel.text.length==0){
// Show alert Select Country
return NO;
}
// Similarly Create method to validate your email
if (![self validateEmailString:self.email.text]){
// Show alert invalid email
return NO;
}
// and so on check your validation
//Finally retrun YES , validation Over
return YES;
}
使用
if ([self validation])
{
// validation over than call your API
[self Account];
}
验证
textfield
的最佳方法是
创建一个验证方法
-(BOOL)validation
{
if (self.name.text.length==0){
// Show alert enter Name
return NO;
}
if ([self.email.text.length==0){
// Show alert enter Email
return NO;
}
if ([_code.titleLabel.text.length==0){
// Show alert Select Country
return NO;
}
// Similarly Create method to validate your email
if (![self validateEmailString:self.email.text]){
// Show alert invalid email
return NO;
}
// and so on check your validation
//Finally retrun YES , validation Over
return YES;
}
使用
if ([self validation])
{
// validation over than call your API
[self Account];
}
1.你在情节提要中指定了哔叽吗?2.我没有在您的函数中看到任何调试
NSLog
。3.输入字段中的值是什么?您被带到下一页可能是因为[self-Account]
。Account
方法是否用于显示下一个屏幕?无论发生什么验证,您的代码都会调用[self-Account]
。为什么你会在1秒后自动关闭任何警报?这可能是因为你直接从情节提要中提供导航。在故事板中检查一次。[自我帐户]在此我的API中正在运行并注册来自用户的数据@TejasK1。你在情节提要中指定了哔叽吗?2.我没有在您的函数中看到任何调试NSLog
。3.输入字段中的值是什么?您被带到下一页可能是因为[self-Account]
。Account
方法是否用于显示下一个屏幕?无论发生什么验证,您的代码都会调用[self-Account]
。为什么你会在1秒后自动关闭任何警报?这可能是因为你直接从情节提要中提供导航。在故事板中检查一次。[自我帐户]在此我的API中正在运行并注册来自用户的数据@泰哈斯克汉克斯很多@非常感谢@谢天谢地,我得到了答案@谢谢,我得到了答案@迪鲁