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

JavaScript jQuery Basics (2014) Creating a Password Confirmation Form Perfect

JQuery Basics: Extending 'Perfect'

In an effort to experiment and improve the validation a little more (and for some fun), I added three new behaviors to the code:

1) Prevent the cursor from becoming a 'hand' whenever canSubmit() is false

2) Use regular expressions to prevent users from entering disallowed characters

3) Cause a an error 'beep' if the user types an invalid character

(Step 3 required uploading the default Windows Ding .wav file.)

Here's my code: (only included the modified part of the code)

function error_beep() {
  var snd = new Audio("../sound/windows-ding.wav");
  snd.play();
  return false;
}

function swapCursor(){
  // Enable/Disable 'Hand' Cursor
  return canSubmit() ? 'hand' : 'default';
}

function restrictUserInput(key) {
  // Restrict input with RegEx
  const char = String.fromCharCode(key.charCode);
  var regEx = new RegExp(/^[a-zA-Z0-9!@#$%^&*]/);
  if (!regEx.test(char)) {
    error_beep();
    return false;
  }
};

function enableSubmitEvent() {                
  $("#submit").prop("disabled", !canSubmit()).css('cursor', swapCursor());
}

//When event happens on password input
$password.focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent)
.keyup(enableSubmitEvent).keypress(restrictUserInput);

//When event happens on confirmation input
$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent)
.keyup(enableSubmitEvent).keypress(restrictUserInput);

enableSubmitEvent();

One thing I found curious is that the 'key' parameter gets passed to the retrictUserInput function seeming automatically.

Try it.

Enjoy.

-Pete

2 Answers

Steven Parker
Steven Parker
229,732 Points

There's nothing surprising about the argument. According to the documentation for .keypress it takes a handler as an argument. And the handler is defined as a function which takes an event object as a parameter. And of course, that object has a charCode property.

Good job on your enhancements. :+1: And since you used it consistently, the code works as-is; but I couldn't help but think that "retrictUserInput" might be missing a letter "s" in the name (to make it "restrictUserInput").

Thanks for the clarification about handlers - very helpful! Something tells me I could use to spend more time perusing the documentation. Lol.

And good eye. Apparently, I could use the services of a good proofreader, also!?! (Rim-shot!?!) Will fix the code now.

(Fixed, now, btw...)