jQuery Projects: Pocketbook, help needed


#1

Hey guys, for some reason I can only get the password script to work. Nothing else does and I really don't know why. Pointers please ? Thanks !

var main = function() {
  $('form').submit(function() {
    var firstName = $('#first-name').val();
  
    if(firstName === "") {
      $('p.first-name-error').text("Please enter your first name.");
    }
  });
  $('form').submit(function() {  
    var lastName = $('#last-name').val();
    
    if(lastName === "") {
        $('p.last-name-error').text("Please enter your last name.");
    }
  });
  $('form').submit(function() {  
       var email = $('#email').val();
    
     if(email === "") {
        $('p.email-errror').text( "Please enter your email address.");
    }
    
    else if(email === "taken@email.com") {
        $('p.email-errror').text( "This email is already taken.");
    }
  });
  $('form').submit(function() {
    var password = $('#password').val();
    
    if(password === "") {
        $('p.password-error').text("Please enter a password.")
    }
    else if(password.length < 8) {
        $('p.password-error').text( "Short passwords are easy to guess. Try one with at least 8 characters.")
    }
  });
  return false;
}

$(document).ready(main);

#2

Hey John,

As a start, bundle all of your $('form').submit(...);s into one.

$('form').submit(function() {
  // declare each variable

  if (/* condition */) {
    // throw error
  }

  // repeat above if statement 
});

There's no use at all in pointless repetition :slight_smile:
Once you do that and post your updated code, we can continue working on narrowing down to the cause of the problem :slight_smile:


#3

Here you go.

var main = function() {
  $('form').submit(function() {
    var firstName = $('#first-name').val();
  
    if(firstName === "") {
      $('p.first-name-error').text("Please enter your first name.");
    } 
    
    var lastName = $('#last-name').val();
    
    if(lastName === "") {
        $('p.last-name-error').text("Please enter your last name.");
    }

       var email = $('#email').val();
    
     if(email === "") {
        $('p.email-errror').text( "Please enter your email address.");
    }
    
    else if(email === "taken@email.com") {
        $('p.email-errror').text( "This email is already taken.");
    }
 
    var password = $('#password').val();
    
    if(password === "") {
        $('p.password-error').text("Please enter a password.")
    }
    else if(password.length < 8) {
        $('p.password-error').text( "Short passwords are easy to guess. Try one with at least 8 characters.")
    }
  });
  return false;
}

$(document).ready(main);

#4

@goodrij Much better :slight_smile:
The first problem is the project's fault, and not yours. Look at the ids of the <input>s on lines 24 and 29 - instead of #first-name and #last-name, you need to change it to #first and #last. I'll see if I can get the instructions for this fixed :slight_smile:
Then, very carefully check your spelling of .email-errror on line 22.

Fix both of those, and your code will work :slight_smile:


#5

Thanks Zeke! I appreciate the help! :slight_smile:


#6

A post was split to a new topic: It Don't Reply with the Error Message About the Email Address


#7

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