Instructor: Hey hey, welcome back.
In this video we're gonna cover
JSF validation with regular expressions.
We'll cover the following topics.
We'll start off with the regular expression demo
and then we'll validate a field
with the regular expression,
and finally we'll pull it all together
with the full JSF page example.
In here, I'm assuming that you understand
what regular expressions are,
if not you can do a quick Google search
and find out what they are
but here we'll just assume that knowledge
and we'll move forward.
So here's an example of using a regular expression.
So we have a phone number and we're gonna make use
of a regular expression to match on a given format.
So the format is that you're gonna have
three digits with a hyphen,
three more digits with a hyphen, and finally four digits.
So here, I'm just giving all numbers, it doesn't follow
the format, so here they actually give us
the error message for us.
Three digits, hyphen, three digits,
hyphen, four digits.
So I need to fix this by giving the phone number
in the proper format.
So I go through and hit submit
and then this will pass through just fine
because we followed the appropriate pattern
that was set up by the regular expression.
So what I'll do in the next slide
is I'll show you actually how to implement this
using JSF code.
So here's our JSF example.
So we're gonna validate a North American phone number
like I gave you the pattern a little bit earlier.
So the first bullet here we pass
by giving a number with hyphens,
and the second example fails because
we're missing the hyphens or we're
missing the dashes between the digits.
So here's the actual coding for this
as far as making use of a regular expression.
So we start off with the input text field.
So value is studentThree.phoneNumber,
again we're just binding this to the
Managed Bean in the background.
And then the next element here is
a validator message, so this is the actual
error message that appears if the validation fails.
Finally, we have this new item here
called validateRegex.
So this is where you give your special
regular expression pattern.
So here we're basically saying the pattern is three digits
with a dash, three more digits with a dash,
followed by four digits.
That's the pattern that the user must enter
in order for this validation to pass.
Okay, so let's pull it all together with the full example.
So in the top left, we're gonna have our StudenThree form,
we're gonna have first name, last number, and phone number.
Then in the center, we're gonna have a Managed Bean
called StudentThree, and then at the bottom,
we'll have our StudentThree response page.
This is basically our confirmation page
and we'll simply display the phone number for the user.
Alright, let's look at our to do list
as far was what we have to do.
The first thing we'll do is we'll add a new Managed Bean
class called StudentThree.
It's gonna have the first name, last name, and this new
entry for phone number.
Then, we need to update the form to make use
of our validation rule for regular expressions.
Then, we update the confirmation page
to display the user's phone number.
Alright, so this is our game plan.
I'm ready to start coding.
Let's go ahead and jump in.
Alright, so I'm moving into Eclipse.
What we're gonna do here is we're actually
gonna make use of that existing project, validate-demo
and what I'd like to do is move
to the actual source directory,
so Java Resources, source, our package,
validate-demo, and I'm gonna look
at this class called StudentThree.java.
This is creating the Managed Bean.
And there again, we've done a lot of Managed Beans before,
so I'm not gonna start from scratch.
We'll simply look at the class
and look at some of the new fields here.
So StudentThree is a Managed Bean.
We're gonna have three fields here,
first name, last name, phone number,
and phone number's the new field
that we're working on here for handling regular expressions.
Our standard work of having a no argument constructor,
StudentThree has the same name as the class.
And we have our normal getter and setter stuff.
I just wanna highlight the getters
and setters for phone number.
Get phone number and set phone number.
Alright, so our bean is in good shape.
Let's go ahead and move to the next step.
So the next step, we need to update our JSF form.
So I'm moving to the WebContent directory,
I'll find this file, student_three_form.xhtml.
I'll go ahead and open this file.
I'll expand the window here for a second.
And again, this is basically an extension of what we've done
in the previous videos, so I'm not
gonna cover everything here.
We can start with the form, and then I'll simply
highlight this new section here for phone number.
So this phone number's our input text for the user
to enter their data, we're gonna bind this
to studentThree.phoneNumber, our Managed Bean.
And then we'll also specify the validatorMessage.
So in the event that the validation fails,
this is the message that it'll display on the screen.
Now, we validate this field by using
a validateRegex, so the pattern is three digits,
dash three digits dash four digits.
That's the actual pattern that we're using
to validate this given field.
Alright, so this looks really good.
So we can kinda take a look at line 38,
where we simply display the error message
for phone number in the event that the validation fails.
And then just scrolling on down, we move to line 42
for our command button, or our submit button.
And then the action of student_three_response.
And again, like we've already learned,
JSF will call student_three_response.xhtml.
And that's pretty much it for our form.
Good job.
Okay, so now, moving forward, our last step is updating
the confirmation page.
So I'll move back over to the project listing here,
I'll take a look at this file, student_three_response.xhtml,
I'll double-click this file, and go ahead
and open it here in the editor.
And I'll expand the window.
Again, not much here.
Simply on line 12, shows the student's name,
first and last, and on line 16, we display
the student's phone number.
So again, not much really happening here.
We're simply displaying the student's phone number
once they've passed the validation rules.
Alright, so this all looks really good so far.
Good job.
Okay, so I just ran the application.
Here's our form, so I'll go ahead and enter the information
for first name and last name, and then I'll go ahead
and enter a phone number.
And I'll just enter some bad data, hit submit,
and it's gonna give me this error message.
The phone number must follow the format of
three digits, hyphen, three digits, hyphen, four digits.
Alright, so we'll need to follow that appropriate format
for our input data for phone number.
So I'll clear this out and I'll go ahead
and give some good data here.
And then I'll go through and hit the submit button,
and I'll pass all the validation rules,
and I'll make it to the confirmation page.
So this is great.
Alright, so we accomplished some good work here.
So we started off with the regular expression demo,
and then we went through and we validated information
with a regular expression.
And then finally, we pulled it all together
with the full JSF page example.
Great job.
Well, this wraps up the video.
In this video, we learned how to do JSF validation
with regular expressions.