Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Christina Fowler
Courses Plus Student 8,109 PointsEmail validation plugin - code test 2/4 :(
Please can someone help :( I've been stuck on this for two weeks, it's so frustrating.
Task: Open a new script tag and using the "validEmail" plugin, store in a variable "isValid" whether the email address in the text input is valid.
This is what I started with:
var $isValid = $("input");
$isValid.validEmail();
Error: Bummer! null
So I then wrote out more from the video:
var $isValid = $("input");
function requiredFilledIn() {
if(!$isValid.validEmail());
}
$isValid.validEmail({
on:"keyup", success:function(){
$(this).next().addClass("valid");
}, failure:function(){
$(this).next().removeClass("valid");
}
});
requiredFilledIn();
I suspect the add/remove class will be in the next step but I find it hard to do these tests without a visual clue as to whether I've done it right.
Error: Bummer! Null
I tracked down that the problem was with the requiredFilledIn function so that required me to write out more code from the video (and therefore extra mark up) even though it wouldn't be used in this case:
var $isValid = $("input");
var $submit = $(".submit input");
var $required = $(".required");
function containsBlanks(){
var blanks = $required.map(function(){ return $(this).val() == ""});
return $.inArray(true, blanks) != -1;
}
function requiredFilledIn() {
if(containsBlanks() || !$("#email").validEmail())
$submit.attr("disabled","disabled");
}
$isValid.validEmail({on:"keyup", success:function(){
$(this).next().addClass("valid");
}, failure:function(){
$(this).next().removeClass("valid");
}});
requiredFilledIn();
Error: Bummer! Null
Now I understand that this is probably way too much code for this stage of the test but hopefully you can see how I got to this stage.
So right now I have become so frustrated with the error messages that give no help at all I have written out everything from the video and then trimmed everything back as far as I can which leaves me with this:
var $isValid = $("input");
function requiredFilledIn() {
if(containsBlanks() || !$isValid.validEmail());
}
requiredFilledIn();
Error: The validEmail method wasn't called on input
This error confuses me because from what I can work out I have called the validEmail method on input with "!$isValid.validEmail()" (this is what the video shows you to do) so this suggests that the "containsBlanks() ||" is the problem but if I remove "containsBlanks() ||" the error message goes back to null which is why I have kept it in.
Humph :(
4 Answers
Andrew Chalkley
Treehouse Guest TeacherYou're almost there. The code callenge is looking for the result of the validEmail call being stored as isValid not $isValid.
So you could do something like this since $isValid holds the jQuery representation of the input.
var $isValid = $("input");
var isValid = $isValid.validEmail();
Or you can do it on one line like this.
var isValid = $("input").validEmail();
Does that help?
Christina Fowler
Courses Plus Student 8,109 PointsThanks very much, that helps me pass the test but I don't understand the difference between isValid and $isValid.
I thought variables needed to start with a $?
Is there a video to explain that?
Andrew Chalkley
Treehouse Guest TeacherI use the dollar sign $ before variables that represent a jQuery selected element.
For example:
var $requiredFields = $(".required");
Would return the jQuery element whereas if you call a method it may return something else like a "string"
var contentOfTextArea = $("textarea").val();
I use the dollar sign just as a reminder what I'm dealing with.
Imagine I have a text input and a hint next to it like this:
<p>
<input id="name" type="text">
<span class="hint">Enter Name</span>
</p>
I can call the next() method on $("#name") to return a jQuery selected element:
var $hint = $("#name").next();
validEmail() returns a boolean so I wouldn't put a $.
Christina Fowler
Courses Plus Student 8,109 PointsOk, thanks Andrew :)
I remember in one of the early jquery intros you said that a $ was not necessary but that you liked to use it as a reminder, this is a bit clearer now.
I do look forward to when the error messages give more hints, I know you guys are working on that.