Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build an Interactive Website Form Validation and Manipulation Checking Values

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,825 Points

How does isValidEmail method link to the code in this challege?

Hi all,

This is more of an observational point than a question but I'm reviewing my progress so far in the "Build an Interactive Website" course.

In the second stage there's a challenge that asks us to create a method, with the name isValidEmail that passes into it one argument, for which is used the name email

My answer to the challenge is the following.

function isValidEmail(email) {
    return email.indexOf("@") != -1;

and it passes. Cool!

But when I looked at the index.html file in the challenge, there doesn't appear to be an email field. So no real method of checking whether the @ symbol actually appears for a valid email. No default text for it to check.

I'm assuming this is okay for the purposes of this challenge but I just wanted to nail down how this works in practice.

I used email for my argument. Following along in the videos, it uses email for it's for, id and name attributes. Is this how it jQuery figures out which input field to check?

Be good to see peoples thoughts. :-)

1 Answer

In this case, the isValidEmail method doesn't check a particular field. It's a generic function that tests a string, and the source of that string can be pretty much anything — a variable, a string literal, return value of some other method...

The email parameter is the internal name for whatever is passed to the isValidEmail, it's just a way to reference the argument passed to the method inside the method.

To actually get a value from some input field (when using jQuery), you'd want to add an id to that field (or a name, or even a class) so you can select that field (using a jQuery selector) and get its value. Then you'd pass that value to the isValidEmail method.

For example:

<input type="text" name="email" id="email">

With such an input field, you could have this piece of code checking the value

if (isValidEmail($('#email').val())) {
   // do something

or this:

if (isValidEmail($('input[name="email"]').val())) {
   // do something
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,825 Points

Oh I see, so the function is just waiting for an input field to check but hasn't been given one yet. It takes another function to call isValidEmail and then perform the validation.

This makes sense to me thanks for the explanation. :-)