Reputation: 147
I have contact form in my application in that i need to validate the email id i have found some solution for validating the email id but when used the validation method its showing warning.
@synthesize mail;
email validation code:
-(BOOL) Emailvalidate:(NSString *)mail
{
BOOL stricterFilter = YES;
NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSString *laxString = @".+@([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:mail];
}
In the return its showing the warning like
Local declaration of mail hides instance variable.
I am sending all the date's to my server using the JSON
please tell me how to resolve this one.
Upvotes: 1
Views: 7993
Reputation: 2587
The problem is you have an local variable (declared in your object with @syntensize) with the same name as the variable declared in your method, try this:
-(BOOL) validateEmail:(NSString *)tempMail
{
BOOL stricterFilter = YES;
NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSString *laxString = @".+@([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:tempMail];
}
Upvotes: 4
Reputation: 3508
- (BOOL)validateEmailWithString:(NSString*)checkString {
BOOL stricterFilter = NO;
NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSString *laxString = @".+@([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:checkString];
}
Upvotes: 1
Reputation: 1270
In addition to what cania already said, this would make a good class method instead of an instance method. I would make it a class method so that any of your classes can call it easily if you ever need to validate another e-mail.
Then a call would look like this and return true if a valid e-mail, assuming your mail property is a NSString.
if ([YourClassName Emailvalidate:self.mail]) {
// valid e-mail, do what you want to do
}
else {
// handle getting an invalid e-mail here
}
Upvotes: 0
Reputation: 858
Well, your Emailvalidate method takes the parameter named 'mail', and your class already has a field named 'mail' as well... Just change the 'mail' parameter to something like 'newMail'..
Upvotes: 1