Email and password errors


#1


https://www.codecademy.com/en/courses/jquery-ext/projects/jquery-prj_milford-school?link_content_target=interstitial_project


I have the dropdown menu working, but the error messages do not appear when the boxes are blank. I tried some of the code from an earlier similar exercise, but it was either the same or did not work either.

Thanks.


var main = function() {
  $('.dropdown img').click(function() {
    $('.dropdown-menu').toggle(250);
	});
  $('.form').submit(function () {
    var email = $('#email').val();
    var password = $('#password').val();
  
  	if(email === "") {
      $('.email-error').text("Please enter your password.");
    }
    if(password === "") {
      $('.password-error').text("Please enter your password.");
    }
  	return false;
  });

};

$(document).ready(main);


#2

Error message does not match.

Consider, since we are not processing (logging in) then we needn't cache the email and password, just examine them. In the following example we check the length, not the content...

    if ($('#email').val().length === 0){
      $('.email-error').text("Please enter your email");
    } else {
      $('.email-error').text("");
    }

#3

Thanks. My bifocals move things around and I type what I read sometimes lol. I tried this segment in my code, but the error messages still don't come up on an empty box. Here is what I have now:

'''

var main = function() {
 $('.dropdown img').click(function() {
  $('.dropdown-menu').toggle(250);
});
$('.form').submit(function () {
var email = $('#email').val();
var password = $('#password').val();

if ($('#email').val().length === 0) {
  $('.email-error').text("Please enter your email.");
} else {
  $('.email-error').text("");
}
if($('#password').val().length === 0) {
  $('.password-error').text("Please enter your password.");
} 
   else {
   $('.password-error').text("");
   }

return false;
});

 };

$(document).ready(main);

'''


#4

Those two lines can be removed since we do not need to cache the values.


#5

you are trying to select form class however there is no such class so your code does not work.
Try $("form").submit(...) instead of $(.form").submit(...)


#6

Thanks! that did fix it.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.