1

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.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
user3349668
  • 147
  • 2
  • 11
  • 1
    The problem is you have an local variable (declared in your object) with the same name as the variable declared in your method, try to change (NSString *)mail to (NSString *)tempMail – ReeCube Mar 12 '14 at 07:31
  • @ReeCube i have changed the name now how to connect this validation to mail string – user3349668 Mar 12 '14 at 08:41
  • This is because of you have declare the same variable name in global scope. – Vineesh TP Nov 13 '14 at 11:06

4 Answers4

4

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];
 }
Jamie Chapman
  • 4,229
  • 5
  • 29
  • 47
ReeCube
  • 2,545
  • 17
  • 23
  • how to connect my mail uitextfield and the validation pls tell me – user3349668 Mar 12 '14 at 08:44
  • Add an change event and then each time the content changes, you have to check it, more about the change event: http://stackoverflow.com/questions/7010547/uitextfield-text-change-event – ReeCube Mar 12 '14 at 08:45
1

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'..

cania
  • 857
  • 10
  • 16
1
- (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];
}
jki
  • 4,617
  • 1
  • 34
  • 29
Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
0

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 
}
Inertiatic
  • 1,270
  • 1
  • 9
  • 12