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