Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Validate a phone number, ensuring that it's entered following the pattern `(555) 555-5555`.
-
0:00
In the comments, you can see the requirements for
-
0:02
the phone number validation.
-
0:05
Go ahead and pause this video,
-
0:07
see if you can complete this function before I show you my solution.
-
0:12
One quick reminder though, don't forget that parentheses are special characters.
-
0:18
So to indicate you want to match those literal characters,
-
0:21
you need to escape them with a backslash, like this or like that.
-
0:29
Here's how I broke down this problem.
-
0:31
I see three groups of numbers, two groups of three and one group of four.
-
0:39
So I'll type return.
-
0:44
And three groups.
-
0:49
And one group of four.
-
0:51
There are some characters that we want the user to type in though.
-
0:54
Parentheses around the first group.
-
0:58
So, \ (, and
-
1:02
\), a space, and -.
-
1:09
And we don't want any other characters at the beginning or end.
-
1:11
So I'll put a ^ and $ at either end.
-
1:19
And we'll test, and pass in telephone.
-
1:24
Save, and if I test this out in the browser,
-
1:37
We can see it works!
-
1:38
Asking a user to format a phone number is unnecessary though.
-
1:42
I'll show you how you can format the phone number automatically in a future video.
-
1:48
Next let's validate the email address.
You need to sign up for Treehouse in order to download course files.
Sign up