Hi, Alex here from rebeloper.com and welcome back to the channel. Now in this
tutorial series, yes it is a series and this is the first video in the series.
We are going to talk about validation. Yes that is correct.
Text field validation in Swift. So without further ado let's get right into it.
Okay, let me just show you what the testing project is all about. So let's
build and run and we are calling this regex
mania because this validation is based on regex. Now regex is quite hard to
understand but we are going to handle this in this video. So it says type in
your age. So let's type in not an age so Alex, not a valid age and that is correct.
Now let's type in 12. It is a valid age, great. Now how does this really work?
Well we want to have a two-digit number and that is what we are looking for here
maybe you want to have an 8 that is greater than 18 that can be added to but
this is just for validation. If I type in test here this is not a valid age.
Now this is what we are going to implement today in this video. In the next videos
we are going to implement validation of emails passwords and websites. So go
ahead and do be on the lookout for the next videos in this episode series, okay.
So what we are going to do now is start from the starter project, start from the
starter project that is really funny. Now what you need to do is go ahead and
click the link down in the description you will be taken to our YouTube
resources page where you will be able to find the link
to our github repository and once you clone or download that repository you
will find yourself with this structure. Well you will not have the pods and the
pod file lock but you will have a pod file. Now all you have to do is update
the pod file, so open up terminal here. Let's make that a little bigger like so
and all you have to do is type in cd a space and grab the root folder of your
project and hit drag and drop it on to the terminal window and hit return.
Now finally you need to type in pod update and that will update all of the pods in
your project. I won't do this because it takes quite a while but once you do that
you will have all of your pods installed well there is only one pod
and that is tiny constraints and of course that is for constraining our
views. I always use tiny constraints in my projects. Go ahead and take a look at
my video on how to use tiny constraints on the channel, okay. And now once you open
that up you will find yourself with this structure. We have an app delegate
that has our root view controller inside a navigation controller. Here we have our root
view controller with the text field and a label and a navigation item title and a
handle text change and that is the selector of our text field, okay. And we
will print out the text and I have an extensions file here. You can take a
look at the rejects expressions cheat sheet here, it
will come really handy to understand all of the regexes that we are going to
create in these episodes, okay. Now what we are going to do now is let's just build
and run and see what happens. Well basically we will have our text field
with type in your age and that is it. If we type in anything so
hello there you will find that all of this is typed out into, it is printed out into
the console and that is it. Now let's get down to business and create our regexes.
So we want to validate these text fields text and that text field text is a
string. So now that we want to validate this text that is a string we might as
well create an extension for it. So let's go and select the extensions
dot Swift file and go ahead and create a string extension here and that is it we
want to have a function let's call this function something like and it will
return a bool value. And that is it. Once you do have this of course we have to
have a return value let's this to true for now so we can get rid of this error.
So if we go back to our root view controller and let's say if text dot is
where I need to hit command B so we get the completions here. So is valid then
let's print out valid text, okay. And otherwise it is not valid. So that is
what we are going to add in here and of course we want to have more validation
types so let's create an enum here of validity type, okay. Enum ... there we go.
Enum validity type and the first case is age and in the next videos we are
going to talk about emails so we might as well type in here email just so we
know that we have more than one cases I will comment this out we will talk about
that in the next video. Okay, now we want to check if is valid
and we are going to add in here of validity type and it will be of type
validity type. I will make this small v and now we need to return something
according to this validity type, okay. Now let us just take a look at how regex works.
And we are going to return here an NSPredicate and we are going to use
this format with arguments, okay. And format, the format will be some sort of
strings so let's create a format here, let
format equals and I will add in here empty string adjust for. Now and the
argument is actually the regex. So let's create our regex here and I will
make this a variable of course again another empty string because the
variable will be changed according to our validity type and what we want to do
next is type in a dot evaluate weight and self because self is the string that
we are adding in here, okay. Now it says the rejects has was never mutated.
So let's fix this by adding here a switch statement, switch and value is validity
type and the pattern for now it is age and dot age and that's all to it right now
we will add in later on for email password and website but for now the age
regex will be so let's type in here regex equals to and some other value.
Now let's talk about the format and regex. Now the format and I will need
to copy and paste this because I don't want to have typos here. Now the format
will be like so. So it does match the regex, okay let me just paste that in.
I will try to write this out. Let me just I believe it is like zero till nine
and let's say two till twenty or something like that, six or rather yet two to till two.
So what does this mean and as you can see I am stumbling with and I will
really make sure that we do have the correct rejects and that is it. I will
just copy and paste it here and I let me just explain what this does.
Well basically we want to have characters ranging from 0 till 9 and the minimum
character count like the strings count is set to be two
and the maximum count for the string has to be two like it has to be a
two-digit number and that is the regex here and for ease of use I will create
another enum here and that will be called regex and the case and it will
be a string enum and for the case age I will just copy and paste, cut and
paste all of this here and for the regex now I will call upon a regex
dot age dot raw value, okay. Let's hit command B so we know that nothing has
been written wrong and it says it's missing argument in parameter and that
is correct. Let add that in here and string validity type
and that is dot age, okay. And let us just go ahead and take a look at this one and
I believe this is quite okay and we are done with the extension so let's talk
about the route view controllers handle text change. Now here we have the text
and first of all let's create a validity type constant or rather yet let's put it
out all the way up here let and it will be of type string dot validity type and
it will have the value of age. That's it so we will be able to change this to
email and so on with a really clarified way, okay. And now all we have to do
here is change this to validity type. But I don't want to have only an if
statement we want to check for our validity type according to there let's
say email or age value. So what we want to do here is type in here switch and
validity type and in case of dot age we want to do the following and I will
remove the default value here because this switch statement will be exhausted.
Now let's take a look at if the text is valid and let just cut this out and paste it
in there then we want to add this text to our label. But in a way that let's say
text is a valid so we want to have our labels text to say valid and we are not
going to type in here age or email or something like that.
We are going to use the validity type. So validity type and that's it otherwise we
are going to set the labels text to be not valid and age or email and so on.
So that will be there, or rather come to think about it we are not using
this age property so, and let me just cut this out and it'll simply get rid of
this switch statement I just made up my mind
that's how coding is. And one last thing that we might want to add here here in
the placeholder of our textview we have all this age written out in raw string.
So what we want to add here again is the validity type. So hopefully that wasn't a
that hard, so let's build and run and test this out. Okay, type in your age.
Works really well with the age and let's type in Alex and not valid age rather yet
let's change this to not a valid age let's build and run again. So Alex not a
valid age. Let's say four it's still not a valid age. Now let's say 25, valid age.
Great, but let's say if you wanted to type in like three hundred and
sixty-seven. Well that is not a valid age. Or what about ninety nine still a valid age.
100, well not a valid age. Not many people get to have 100 years over their
life, okay. So that is validation of our age. Now in the next video you are going
to talk about email validation. As you can see here we have our regex. It is
pretty simple but for the email we need to make some complicated stuff with the
regex. Go ahead take a look at the emailregex.com
cheat sheet just simply hit command and click on the link here and I hope you
enjoyed this video if you did go ahead and give it a thumbs up subscribe to the
channel and hit that notification bell and if you liked this video go ahead and
comment down below what are you using validations for and as always I will see
you in the next one.