0

I have app in which i want that when user enter url in textfield and presses return key it should check if url is valid then load url in web view else show alert enter valid url

In my code it always show Valid URL.

 - (BOOL)textFieldShouldReturn:(UITextField *)textField
  {
   [textField setUserInteractionEnabled:YES];
   [textField resignFirstResponder];

test=textField.text;

NSLog(@"Test is working and test is %@",test);

[self  urlIsValiad:test];

if ([test isEqualToString:@"Valid"]) {
    NSURL *url = [NSURL URLWithString:test]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [webView setScalesPageToFit:YES];         
    [self.webView loadRequest:request]; 
}
 else{

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter Valid URL" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
  return YES;
}
- (BOOL) urlIsValiad: (NSString *) url 
{
NSString *regex = 
@"((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?";
/// OR use this 
///NSString *regex = "(http|ftp|https)://[\w-_]+(.[\w-_]+)+([\w-.,@?^=%&:/~+#]* [\w-\@?^=%&/~+#])?";
  NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

  if ([regextest evaluateWithObject: url] == YES) {
    NSLog(@"URL is valid!");

    test=@"Valid";

  }  else {
    NSLog(@"URL is not valid!");

   test=@"Not Valid";
}

return [regextest evaluateWithObject:url];
  }
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Queen Solutions
  • 253
  • 2
  • 5
  • 17
  • There is already some discussions on it in Past [Check Here](http://stackoverflow.com/questions/1471201/how-to-validate-an-url-on-the-iphone) or [Here](http://stackoverflow.com/questions/833469/regular-expression-for-url) – Gyanendra Singh Sep 05 '13 at 10:53
  • @GyanendraSingh i have also got code from that description then what is problem can you help me out please – Queen Solutions Sep 05 '13 at 10:55

3 Answers3

1
// .h File 
-(BOOL)validateUrl;
.m File
-(BOOL)validateUrl{
    NSString *urlRegEx =  @"((?:http|https)://)?(www\\.)[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
    if ([urlTest evaluateWithObject: self] == YES) {
        NSLog(@"URL is valid!");
    } else {
        NSLog(@"URL is not valid!");
    }
    return [urlTest evaluateWithObject:self];
}
// Main View Controller .m File
if (![txtWebsite.text validateUrl])
    {
        [KWebsiteURLTypeValidation showAsAlert:self];
        return;
    }
0

You can use this method:

- (BOOL) validateUrl: (NSString *) url {
   NSString *theURL =
   @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
   NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", theURL]; 
   return [urlTest evaluateWithObject:url];
   }

From Check URL validity . Hope this helps.

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
0

i've modified your code so please replace this method with your code and it will work superfine

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

   [textField setUserInteractionEnabled:YES];
   [textField resignFirstResponder];
   test=textField.text;

   NSLog(@"Test is working and test is %@",test);

   if ([self  urlIsValiad:test]) {
       NSURL *url = [NSURL URLWithString:test]; 
       NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
       [webView setScalesPageToFit:YES];
       [self.webView loadRequest:request]; 
   } else {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter Valid URL"  message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
       [alert show];
       [alert release];
   }
  return YES;
}
D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30