In this example, you can see that our function is named validateEmailAddress, and it passes the value of emailAddress to the inner parts where we have the regular expression and the test method.
The first thing we run into inside of our function is the regular expression. The constant emailRegex is our regular expression, and this is what it says:
- There will be a group of one or more characters. These characters can be lower case or upper case letters. They can be the numbers 0 through 9. They can also be a period, underscore, percentage symbol, plus sign, or minus sign. We’ll use this for the name part of the email.
- After this group of characters, there will be an @ symbol.
- Next, there is another group of one or more characters. These characters can be lower case or upper case letters. They can be the numbers 0 through 9, or they can be a period or a dash. We’ll use this for the address part of the email.
- After this second group of characters, there will be a . symbol.
- Finally, there will be a group of at least two, but not more than four characters. These characters can be lower case or upper case letters only. We’ll use this for the domain part of the email.
The next part of the function is the test method. The test method states that if the value passed to the function in the variable emailAddress follows the pattern we established in our regular expression emailRegex, return true, otherwise, return false.
Now that we have taken a look under the hood to see what’s happening, let’s try out a few examples to test the results. We’ll enter a value as if we had entered one into a form to see how the code reacts.
In Example 2, we enter firstname.lastname@example.org. In this case, we see a group of lowercase letters, followed by an @ symbol. Next, we have a group of lower case letters containing one dash, followed by a period. If we check this pattern against emailRegex we can see that it will return True. This entry could be an email address.
In Example 3, we enter email@example.com. The only difference between this example and the last is that this one ends with tv instead of com. Tv might seem strange because we don’t see it often, but it passes the two to four character rule set up in emailRegex, and it uses only letters. The test method will return true. This entry could be an email address.
In Example 4, we enter firstname.lastname@example.org. This example is the same as the last two examples, except this time, the entry ends with info instead of tv or com. This example is also perfectly legal and falls within the 2 to 4 letter rule. The test method will return true. This entry could be an email address.
In Example 5, we enter someone.some@domain. This time emailRegex considers someone.some to be the sender. @domain appears to be the beginning of the recipient’s address, but there is no .com, .tv, or .info. Without the email domain, the test method returns false. This entry is not an email address.
In Example 6, we enter someone.some-domain.com. This time, emailRegex considers the entire entry the name because it never encounters an @ to signify the address portion of the email. Without the @ the test method will return false. This entry is not an email address.
In Example 7, we enter someone-some-domain.com. This example is the same as the last one and emailRegex will consider the entire entry the name part of the email. Dashes and periods are allowed in names. Without the address and domain parts of the email, the test method will once again return false. This entry is not an email address.
In this final example, we enter @some-domain.com. This time the entry begins with an @, but emailRegex is expecting at least one character before the @ to use as a name. Without a name portion of the email, the test method will return false. This entry is not an email address.