1 00:00:00,190 --> 00:00:03,460 Our last field to validate is an email address. 2 00:00:03,460 --> 00:00:06,740 The basic format for an email address is simple. 3 00:00:06,740 --> 00:00:11,390 A string of characters followed by an @ sign, another string of characters, 4 00:00:11,390 --> 00:00:15,430 a dot, and then some top-level domain like com or net. 5 00:00:15,430 --> 00:00:17,230 Why don't you pause the video here and 6 00:00:17,230 --> 00:00:20,660 try to complete the email validation yourself first? 7 00:00:20,660 --> 00:00:22,000 Then, I'll show you my solution. 8 00:00:23,090 --> 00:00:24,310 So to start with, 9 00:00:24,310 --> 00:00:29,100 I'm going to add the things that we know we're gonna have to have. 10 00:00:29,100 --> 00:00:34,140 So I'll put a regular expression, and then I'll call test on it, and 11 00:00:34,140 --> 00:00:39,870 pass in email, and then I wanna return this from our function. 12 00:00:41,350 --> 00:00:46,580 So emails normally have letters and numbers before the @ symbol, 13 00:00:46,580 --> 00:00:51,510 as well as underscores, so we could match repeated word characters at the beginning. 14 00:00:54,520 --> 00:00:58,330 But I've also seen them with dots and pluses. 15 00:00:58,330 --> 00:01:03,100 Perhaps the safest way to match that first segment is anything that is not 16 00:01:03,100 --> 00:01:04,676 an @ symbol. 17 00:01:07,407 --> 00:01:11,850 Notice I'm using a plus symbol for the repetition instead of an asterisk. 18 00:01:11,850 --> 00:01:15,770 That's because there needs to be at least one character before the @ sign. 19 00:01:15,770 --> 00:01:20,120 Any characters between the @ symbol and the dot should be letters, but 20 00:01:20,120 --> 00:01:22,190 maybe numbers are allowed. 21 00:01:22,190 --> 00:01:25,970 To be safe, I'll just match anything that's not an @ symbol or a dot. 22 00:01:31,450 --> 00:01:35,701 For the final stretch, I'm pretty sure that only letters are allowed, 23 00:01:35,701 --> 00:01:37,730 so I'll just use a character set. 24 00:01:41,470 --> 00:01:45,290 Also, emails are case insensitive. 25 00:01:45,290 --> 00:01:49,090 I could add a capital A to Z inside the character set to account for 26 00:01:49,090 --> 00:01:52,370 this, but I'll just set an i flag for this example. 27 00:01:53,610 --> 00:01:56,430 Remember, this case insensitive flag 28 00:01:56,430 --> 00:02:00,470 makes the parser disregard case when it looks for matches. 29 00:02:00,470 --> 00:02:03,830 Finally, let's add a carrot and dollar sign as we've done before. 30 00:02:09,496 --> 00:02:14,690 I'll save and refresh the browser. 31 00:02:14,690 --> 00:02:23,220 And I'll type person@example.com, and it works. 32 00:02:23,220 --> 00:02:26,360 This regular expression will match most emails. 33 00:02:26,360 --> 00:02:30,950 Developers have some disagreement around how to validate emails, though. 34 00:02:30,950 --> 00:02:34,370 Some say there are so many validations that the only way to 35 00:02:34,370 --> 00:02:38,790 really know if you have a valid email address is to send mail to it. 36 00:02:38,790 --> 00:02:43,110 So a production app might have some minimal validation that uses an expression 37 00:02:43,110 --> 00:02:44,420 like this one. 38 00:02:44,420 --> 00:02:50,530 Then, it might send a verification email to ensure the address' validity. 39 00:02:50,530 --> 00:02:51,980 Other developers disagree, 40 00:02:51,980 --> 00:02:55,710 believing that the best way to validate is in the browser. 41 00:02:55,710 --> 00:02:58,595 Here's a site that aims to account for 42 00:02:58,595 --> 00:03:04,557 every possible email address with Regex that also excludes all invalid ones. 43 00:03:04,557 --> 00:03:08,385 There's a lot of good information here that I'll let you read through on 44 00:03:08,385 --> 00:03:08,960 your own. 45 00:03:08,960 --> 00:03:15,610 HTML also supports basic validation of a specific email input type. 46 00:03:15,610 --> 00:03:18,390 See the teacher's notes for more information. 47 00:03:18,390 --> 00:03:22,970 So far, we've been testing strings to see if they match expressions. 48 00:03:22,970 --> 00:03:25,780 Next, let's look at how we can go further and 49 00:03:25,780 --> 00:03:29,450 replace parts of strings using regular expressions. 50 00:03:29,450 --> 00:03:30,010 See you there.