Hi, Alex here from rebeloper.com and welcome back to the YouTube channel and
welcome back to this last episode of regex mania and in this one we are
going to validate websites in UITextField. So without further ado let's jump right into it.
Okay! Now if you are new to this episode series then go ahead and
take a look at the previous ones and they are quite a few we have validated
age, email and password. So go ahead and take a look at that because we are going
to jump right into the example project and if you haven't already done so go
ahead and check out the link down below in the description where you can
download the starter project. So you can follow along these tutorial series, okay.
Now what we have built out in the previous video is password validation.
And we needed to type in a valid password here and this is a valid
password because it has capital letter, small letters, special character numbers.
Now this is a valid password because it has capital letters, small letter special
character numbers and it is at least six characters along, okay. This is a strong
password. Now today we are going to implement some website validation.
So let's just jump right into it and as usual if you are familiar with these
series let's add a validity type case and website and of course if you don't
know what I'm talking about please take a look at the previous
videos because I don't want to go through the project over and over again.
We are just expanding on the previous lessons, okay. In the switch statement we
need to add another case and I'm just going to copy this out and let's add the
case website here and of course we don't want to have the same regex for the
password and website. So let us just create a new
regex case here. Case website equals to end just an empty string for now, okay.
Let's just copy that out and change this regex there, okay. And while we are at it
let's finalize the loop by having the validity type set to website, great.
Let's go back and take a look the yummy regex, okay. And as usual I won't bother
typing this out I'm just simply going to copy it from our test project and as you
can see it's quite a lot, okay. Let me just copy everything out with the
comments too, okay. Because I have some good news we are not
going to have only the simple any site URL validation we are going to dive
deeper yes. So here we have our website and as you can see it's quite a long and
cumbersome regex. Go ahead and take a look at the expression cheat sheet and
the link just hit command and click there you will be taken to this awesome
site page where you can take a look at the expressions, okay. Let us just maybe
go through it and as you can see we have some options for HTTPS, HTTP. If it's not
there is question mark then it's optional. We have an optional WWW and
here we have our body of the site URL. Now this regex is for any site URL.
Okay! Let's check this out but before we do that let me just go back to our root
view controller and as you can see in the extension we have HTTPS and HTTP with a
small letter. So it's not capital and ww too and in the root view control text
field the text field auto capitalizes by
default. So let's set the auto capitalization type to be none.
Okay! So let's build and run and see if we do get our website validated. So type
in your website HTTPS rebeloper and dot and here we go C O M, so com would be a
valid website but we have rebeloper.com and what if we wanted to access the blog.
So we would type in news and that is still a valid website. So go ahead and
take a look at our newly created blog. Okay and that is it. What if we wanted to
add in here www it's still a valid website . But what if we have something
like I don't really know what could make this worse but that is it, but maybe I
do know that a valid web site as you can see ttths
it's not working, okay. So that is how we can validate a website with any sub
pages and as a bonus I am going to show you how to validate other types of
websites. So let me just copy all of this out because again these are regexes
and there are very very typo. So there we go let's comment out this first
one and let's add in here our case, okay. Now any site URL with dot com or dot org
and sub pages. So if we type in here dot co that will still not work and here is
what you need to take a look at for this example, okay. Let's build run and test
that out too, okay. Rebeloper dot and
C O that is still not enough and com a valid website, great. Now what if we
wanted to have dot org still a valid website but net not a valid web site.
If you want to have this rule that only dot com or dot org or whatever type of
websites you want to have validated then this is a good regex for them.
So let's comment this out and take a look at the next one and with this one
we have any site URL with dot com or dot org and no sub pages. Now I didn't go into
sub pages in the previous one or are either yet let me just go into it so you
may see it. There we go. In the simulator dot com and news. It's still a valid web
site. But if we change that and have no sub pages and this regex
does just that. Let's check that out if we type in here
rebeloper dot com it's a valid website, great or org it's still a valid web site.
Let's go with com. But if we try to add a sub page like news our blog feed, news.
That is not a valid website in the way we want to set our regex up, okay.
So finally let's take a look at another case and that is any site URL
with only org and no sub pages ,okay. Here we have our org and no sub pages, okay.
Let's build and run finally and now rebeloper.com will not work only rebeloper.org
Okay! Let's go rebeloper.com it's still not a valid web
site, dot org valid web site. Now you could have any reason to choose any of
these I will just simply comment out this one and leave the any site URL
regex but go ahead and combine all of these in for your use cases, okay.
Now I hope you enjoyed this video about web site validation in UITextFields and of
course some of this was in Swift as usual and I hope you enjoyed the video
and the whole tutorial series if you are new here go ahead and subscribe to the
channel and if you liked the video like the video and hit that notification bell
to get notified when I upload new videos. Now this is the last video in our
regex mania playlist series. Now if you have any questions or issues about
regexes go ahead and comment down below in the comment section I use
answer them within 24 hours and I really like to chat with you guys in the
comment section of our videos. Now as usual I will see you in the next one.