How to validate Video URLs in Dart?

How to validate Video URLs in Dart?

Hello 🙋🏻‍♂️Flutter Developers,

Do you know how to validate whether the link entered by the user is a valid video URL or not?

Yes, we can validate them using Regular Expression.

What is Regular Expression?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in the text. Usually, such patterns are used by string-searching algorithms for “find” or “find and replace” operations on strings, or for input validation.

Source: Wikipedia

Video Platforms follow a pattern for their content URLs. For that, we take the example of YouTube:

YouTube’s URL Pattern for Videos starts with youtube.com/watch?v= followed by a unique video id.

We can use RegExp Class to identify the String that the user has entered, it compares the String with a predefined pattern & returns a bool value as a result of the comparison.

Why Video URL Validator?

If you are wondering about this question, We have faced an issue while developing one of our projects. We were having a screen in the app where the user inputs a Video URL from any popular video platform(For example YouTube) We were not able to check that the Video URL Field has an actual valid value or user had entered some URL that is outside of the platform.

We tried finding a solution to validate Video URL but unfortunately, there were none. To overcome this situation, we think of developing the logic for the same from our side & we started testing multiple regex patterns with various short and long video links provided.

Finally, we managed to create a single pattern that can validate the URL of one PLatform whether it be a long or a short format.

After achieving the same for all 5 Platforms, we decided to wrap our dart logic into a package.

That's How We (Flutter Developers at 7Span) have developed a package that contains some predefined patterns to validate Video URLs from some of the popular Video Platforms.

Installation & Usage

Follow these steps to use this package in your app.

First, add this dependency in your pubspec.yaml file & run the flutter pub get command.

video_url_validator: 1.0.3

This package has methods for each pre-defined platform. For example, If you want to validate the Youtube Video URL:

// You can create instance of VideoLinkValidator Class
VideoLinkValidator validator = VideoLinkValidator();

// Call validate Method & Pass the URL as an argument in String format to the method
bool result = validator.validateYouTubeVideoURL(yourURL);

// You will get the result in Boolean Value
print("Valid: $result");

Currently, it supports the following platforms:

  1. YouTube

  2. Vimeo

  3. Facebook

  4. Dailymotion

  5. Wistia

That's all folks. Thanks for Reading! 🙌🏻

Feel free to ask if you have any doubts / or if I have missed something.

Do Like 👏 If you find this article helpful 🙌🏻

You can connect with me on Twitter / LinkedIn