user2389500
user2389500

Reputation: 25

how to validate youtube URL in objective-C?

I am trying validate youtube url http://www.youtube.com/watch?v=UdQfR4nsXvI .using below code

(BOOL) validateUrl: (NSString *) candidate
{

NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";

NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];

return [urlTest evaluateWithObject:candidate];

} 

i want also validate any youtube URL format ....

edited code:


- (IBAction)uplaod_video_Action:(id)sender
{
    NSURL *candidateURL = [NSURL URLWithString:youtube_Url.text];

    // WARNING > "test" is an URL according to RFCs, being just a path
    // so you still should check scheme and all other NSURL attributes you need

    if (candidateURL && candidateURL.scheme && candidateURL.host)
    {
        // candidate is a well-formed url with:
        //  - a scheme (like http://)
        //  - a host (like stackoverflow.com)
        NSLog(@"hallo");
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Please enter the Email" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

    }

Upvotes: 1

Views: 1850

Answers (3)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Try use this one

-(BOOL) validateUrl: (NSString *) candidate
{

NSString *urlRegEx = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";

NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];

return [urlTest evaluateWithObject:candidate];

} 

Upvotes: 0

Vaibhav Saran
Vaibhav Saran

Reputation: 12908

try this

NSString *urlRegEx = @"(^http:\/\/(?:www\.)?youtube.com\/watch\?(?=[^?]*v=\w+)(?:[^\s?]+)?$);

Upvotes: 0

Stavash
Stavash

Reputation: 14304

Use regexpal.com to check your validation mechanism

enter image description here

There's a very comprehensive regex tutorial on Raywenderlich.com

Upvotes: 4

Related Questions