使用以下代码我正在尝试验证电子邮件textFields,但问题是,即使文本字段中的电子邮件和确认电子邮件文本相同,它也会显示错误消息,表明它们不相同。不知道我的代码有什么问题。有什么帮助吗?
- (IBAction)submit_Act:(id)sender {
//new//
NSString *errorMessage;
errorMessage = @"";
if ([usernametxt.text isEqualToString:@""]){
errorMessage = @"Please enter Username \n";
}
if([passwordtxt.text isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter Password\n"];
}
if([confirmPasswordtxt.text isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter Confirm Password\n"];
}
if((passwordtxt.text == confirmPasswordtxt.text) == false){
errorMessage = [errorMessage stringByAppendingString:@"Password and Confirm Password are not same.\n"];
}
NSString *regex = @"[^@]+@[A-Za-z0-9.-]+\\.[A-Za-z]+";
NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if([emailaddtxt.text isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter Email Address\n"];
}
else if ([emailPredicate evaluateWithObject:emailaddtxt.text] == NO) {
errorMessage = [errorMessage stringByAppendingString:@"Please enter valid Email Address\n"];
}
if([confirmEmailtxt.text isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter Confirm Email Address\n"];
}
else if ([emailPredicate evaluateWithObject:confirmEmailtxt.text] == NO) {
errorMessage = [errorMessage stringByAppendingString:@"Please enter valid confirm Email Address\n"];
}
if((emailaddtxt.text == confirmEmailtxt.text) == false)
{
errorMessage = [errorMessage stringByAppendingString:@"Email and Confirm Email are not same.\n"];
}
if([citytxt.text isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter City\n"];
}
if([statetxt.text isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter State\n"];
}
if([countrytxt.text isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter Country\n"];
}
if (_agree_btn.selected==false) {
errorMessage = [errorMessage stringByAppendingString:@"Please agree Terms & Conditions\n"];
}
if (![errorMessage isEqualToString:@""]) {
NSLog(@"not done");
[[[UIAlertView alloc] initWithTitle:nil message:errorMessage delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show];
return;
}
} else{
NSLog(@" Done Dana done");
_activityIndicator.hidden=NO;
[_activityIndicator startAnimating];
protocolTest = [[ClassWithProtocol alloc] init];
[protocolTest setDelegate:self];
NSString *post = [NSString stringWithFormat:@"task=webapi.registerClient&username=%@&password=%@&person_name=%@&email=%@&terms_check=1&city=%@&state=%@&country=%@&user_id=%@",usernametxt.text,passwordtxt.text,contactPersonstxt.text,emailaddtxt.text,citytxt.text,statetxt.text,countrytxt.text,_fetchid];
[protocolTest startLogintask:post];
}
答案 0 :(得分:0)
而不是
if((emailaddtxt.text == confirmEmailtxt.text) == false)
{
errorMessage = [errorMessage stringByAppendingString:@"Email and Confirm Email are not same.\n"];
}
将它们比作
if(![emailaddtxt.text isEqualToString:confirmEmailtxt.text])
{
errorMessage = [errorMessage stringByAppendingString:@"Email and Confirm Email are not same.\n"];
}
答案 1 :(得分:0)
试试这个,
if([passwordtxt text] != [confirmPasswordtxt text])
{
errorMessage = [errorMessage stringByAppendingString:@"Password and Confirm Password are not same.\n"];
}
发送电子邮件,
if([emailaddtxt text] != [confirmEmailtxt text])
{
errorMessage = [errorMessage stringByAppendingString:@"Email and Confirm Email are not same.\n"];
}
这样做会很好。
答案 2 :(得分:0)
检查以下代码:
if(![emailaddtxt.text isEqual:confirmEmailtxt.text])
{
errorMessage = [errorMessage stringByAppendingString:@"Email and Confirm Email are not same.\n"];
}
答案 3 :(得分:0)
这里的问题是textFields有@和。标志无法直接比较。我们需要首先在字符串中转换它们并使用isEqualToString进行比较,所以这里是我在问题上找到的解决方案。
NSString *emailID = emailaddtxt.text;
NSString *confemail = confirmEmailtxt.text;
NSString *regex = @"[^@]+@[A-Za-z0-9.-]+\\.[A-Za-z]+";
NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if([emailID isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter Email Address\n"];
NSLog(@"email is%@",self.emailaddtxt);
}
if ([emailPredicate evaluateWithObject:emailID] == NO) {
errorMessage = [errorMessage stringByAppendingString:@"Please enter valid Email Address\n"];
}
NSString *regex2 = @"[^@]+@[A-Za-z0-9.-]+\\.[A-Za-z]+";
NSPredicate *confemailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex2];
if([confemail isEqualToString:@""]){
errorMessage = [errorMessage stringByAppendingString:@"Please enter Confirm Email Address\n"];
NSLog(@"email is%@",self.confirmEmailtxt.text);
}
if([confemailPredicate evaluateWithObject:confemail] == NO)
{
errorMessage = [errorMessage stringByAppendingString:@"Please enter valid confirm Email Address\n"];
}
if(![emailID isEqualToString:confemail])
{
errorMessage = [errorMessage stringByAppendingString:@"Email and Confirm Email are not same.\n"];
NSLog(@"email is%@& confemail is%@" ,self.emailaddtxt.text,self.confirmEmailtxt.text);
}